3

私は次の小さなクラスを持っています:

/// RAII wrapper for a Lua reference
class reference
{
public:
    /// Construct empty reference
    reference() : m_L(NULL), m_ref(LUA_NOREF) {}

    /// Construct reference from Lua stack
    reference(lua_State* L, int i = -1) : m_L(L) {
        lua_pushvalue(L, i);
        m_ref = luaL_ref(L, LUA_REGISTRYINDEX);
    }

    /// Destructor
    ~reference() {
        if (m_L) luaL_unref(m_L, LUA_REGISTRYINDEX, m_ref);
    }

    /// Copy constructor
    reference(const reference& r) : m_L(r.m_L) {
        r.push();
        m_ref = luaL_ref(m_L, LUA_REGISTRYINDEX);
    }

    /// Move constructor
    reference(reference&& r) : m_L(r.m_L), m_ref(r.m_ref) {
        r.m_L = NULL; // make sure r's destructor is NOP
    }

    /// Assignment operator
    reference& operator=(reference r) {
        swap(r, *this);
        return *this;
    }

    /// Swap with other reference
    friend void swap(reference& a, reference& b)
    {
        std::swap(a.m_L,   b.m_L);
        std::swap(a.m_ref, b.m_ref);
    }

    void push() const { lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_ref); }

private:        
    lua_State* m_L;
    int        m_ref;
};

代入演算子は、コピーアンドスワップイディオムを使用して実装され、右辺値とともに使用される場合は、moveコンストラクターを呼び出すことになっていることに注意してください。

ただし、reference r; r = reference(L);代入演算子を入力する前に、コピーコンストラクターを呼び出します。なんで、なんで?

2つの代入演算子を書くと、次のことが役立ちます。

    /// Assignment operator
    reference& operator=(const reference& r) {
        reference copy(r);
        swap(copy, *this);
        return *this;
    }

    /// Move assignment operator
    reference& operator=(reference&& r) {
        swap(r, *this);
        return *this;
    }

ただし、コピーの省略を無効にするという犠牲を払って。

値渡しは期待どおりにここで機能するはずではありませんか?または、私のコンパイラ(MacではClang)でさえ壊れていますか?

アップデート:

次の小さなテストケースは正しく機能します。

#include <iostream>

using namespace std;

struct resource
{
    resource(int i=1) : i(i) { print(); }
    ~resource() { print(); i = 0; }

    void print() const
    {
        cout << hex << " " << uint16_t(uintptr_t(this)) << ") " << dec;
    }

    int i;
};

resource* alloc_res()
{
    cout << " (alloc_res";
    return new resource(0);
}

resource* copy_res(resource* r)
{
    cout << " (copy_res";
    return new resource(r->i);
}

void free_res(resource* r)
{
    if (r) cout << " (free_res";
    delete r;
}

struct Test
{
    void print() const
    {
        cout << hex << " [&="   << uint16_t(uintptr_t(this))
             << ", r=" << uint16_t(uintptr_t(r)) << "] " << dec;
    }

    explicit Test(int j = 0) : r(j ? alloc_res() : NULL) {
        cout << "constructor"; print();
        cout << endl;
    }

    Test(Test&& t) : r(t.r) {
        cout << "move"; print(); cout << "from"; t.print();
        t.r = nullptr;
        cout << endl;
    }

    Test(const Test& t) : r(t.r ? copy_res(t.r) : nullptr) {
        cout << "copy"; print(); cout << "from"; t.print();
        cout << endl;
    }

    Test& operator=(Test t) {
        cout << "assignment"; print(); cout << "from"; t.print(); cout << "  ";
        swap(t);
        return *this;
        cout << endl;
    }

    void swap(Test& t)
    {
        cout << "swapping"; print();
        cout << "and"; t.print();
        std::swap(r, t.r);
        cout << endl;
    }

    ~Test()
    {
        cout << "destructor"; print();
        free_res(r);
        cout << endl;
    }

    resource* r;
};

int main()
{
    Test t;
    t = Test(5);
}

clang++ --std=c++11 -O0 -fno-elide-constructors test.cpp -o test移動コンストラクターでコンパイルされた場合、が呼び出されます。(切り替えてくれてありがとう、ベンジャミン・リンドリー)

問題は今です:なぜ今それが機能するのですか?違いは何ですか?

4

1 に答える 1

1

でコピー コンストラクターの呼び出しを引き起こす正当な C++11 状況はありませんr = reference(L);

これは実質的に と同等r.operator =(reference(L));です。パラメータを値で受け取るためoperator=、次の 2 つのうちのいずれかが発生します。

  1. 値を構築するためにテンポラリが使用されます。これは一時的なものであるため、の移動コンストラクターを優先的に呼び出し、移動を引き起こします。reference
  2. 一時的なものは、値引数に直接省略されます。コピーや移動はありません。

この後、operator=が呼び出されますが、これは内部的にコピーを行いません。

したがって、これはコンパイラのバグのようです。

于 2012-04-24T15:08:49.180 に答える