c++ - 如何让这段涉及 unique_ptr 的代码进行编译?

标签 c++ c++11 rvalue-reference unique-ptr

#include <vector>
#include <memory>

using namespace std;

class A {
public:
    A(): i(new int) {}
    A(A const& a) = delete;
    A(A &&a): i(move(a.i)) {}

    unique_ptr<int> i;
};

class AGroup {
public:
    void                    AddA(A &&a) { a_.emplace_back(move(a)); }

    vector<A> a_;
};

int main() {
    AGroup ag;
    ag.AddA(A());
    return 0;
}

不编译...(说 unique_ptr 的复制构造函数被删除)

我尝试用 forward 替换 move。不确定我这样做是否正确,但它对我不起作用。


[~/nn/src] g++ a.cc -o a -std=c++0x
/opt/local/include/gcc44/c++/bits/unique_ptr.h: In member function 'A& A::operator=(const A&)':
a.cc:6:   instantiated from 'void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = A, _Tp = A, _Alloc = std::allocator<A>]'
/opt/local/include/gcc44/c++/bits/vector.tcc:100:   instantiated from 'void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = A, _Tp = A, _Alloc = std::allocator<A>]'
a.cc:17:   instantiated from here
/opt/local/include/gcc44/c++/bits/unique_ptr.h:219: error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = int, _Tp_Deleter = std::default_delete<int>]'
a.cc:6: error: used here
In file included from /opt/local/include/gcc44/c++/vector:69,
                 from a.cc:1:
/opt/local/include/gcc44/c++/bits/vector.tcc: In member function 'void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = A, _Tp = A, _Alloc = std::allocator<A>]':
/opt/local/include/gcc44/c++/bits/vector.tcc:100:   instantiated from 'void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = A, _Tp = A, _Alloc = std::allocator<A>]'
a.cc:17:   instantiated from here
/opt/local/include/gcc44/c++/bits/vector.tcc:314: note: synthesized method 'A& A::operator=(const A&)' first required here

最佳答案

可能您的标准库(还)没有定义 unique_ptr<T>::unique_ptr(unique_ptr &&) .我在 4.5 中检查了我的 header ,它就在那里,所以也许可以尝试升级。

当它找不到移动构造函数时,它会寻找复制构造函数并发现它被删除了。

不过,我在编译时遇到了其他错误。

编辑:开始工作了。我不明白你为什么要 move一个已经是右值引用的对象,但你这样做了。唯一的问题是缺少赋值运算符。

#include <vector>
#include <memory>

using namespace std;

class A {
public:
    A(): i(new int) {}
    A(A const& a) = delete;
    A &operator=(A const &) = delete;
    A(A &&a): i(move(a.i)) {}
    A &operator=(A &&a ) { i = move(a.i); }

    unique_ptr<int> i;
};

class AGroup {
public:
    void                    AddA(A &&a) { a_.emplace_back(move(a)); }

    vector<A> a_;
};

int main() {
    AGroup ag;
    ag.AddA(A());
    return 0;
}

关于c++ - 如何让这段涉及 unique_ptr 的代码进行编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2687342/

相关文章:

c++ - 这是对 move 语义的滥用/误用吗?

传递数值时 fstream.h 中函数的 C++ 可移植性问题

c++ - 当检查是eof时,使用getline读取文本文件会导致无限循环

c++ - 如何包装 C++11 回调?

windows - 终端中的 Ctrl+Z 行为

c++ - move 自定义容器的构造函数?

c++ - 在 C++ 中,说一个变量具有右值引用类型有意义吗?

c++ - 使用 libgit2 在特定目录上调用 git diff

c++ - integral_constant 与 constexpr

c++ - 在 C++ 中将非常量引用传递给右值