[英]std::vector<std::unique_ptr<int> > does not compile
When I declare a vector of unique_ptr's, I get this kind of error: 当我声明一个unique_ptr的向量时,会出现这种错误:
d:\qt\mingw64\include\c++\4.8.0\bits\stl_construct.h:75: error:
use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(
const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]'
Which looks like the classical error of creating a containers of objects which have no copy constructor. 这似乎是创建没有复制构造函数的对象容器的经典错误。
However, it is documented in everything I could find that a standard container of unique_ptrs works thanks to the c++11 move semantics. 但是,在我能找到的所有内容中都记录了该文档,由于c ++ 11 move语义,一个unique_ptrs的标准容器可以工作。
I am compiling with MinGW-gcc 64-bit, using -std=gnu++11. 我正在使用-std = gnu ++ 11使用MinGW-gcc 64位进行编译。
Is it supported only in c++11 and not in gnu++11? 它仅在c ++ 11中受支持,而在gnu ++ 11中不受支持吗?
Thanks 谢谢
The following will compile with C++11. 以下将使用C ++ 11进行编译。
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
int main()
{
std::vector<std::unique_ptr<int> > asdf;
return 0;
}
The problem was not std::vector<std::unique_ptr<int> >
itself, but the member variable of this type declared in a copiable class. 问题不在于
std::vector<std::unique_ptr<int> >
本身,而是在可复制类中声明的这种类型的成员变量。 Since the default copy constructor of the class calls the copy constructor of std::vector, which in turns calls the default constructor of std::unique_ptr, the later being deleted, compilation fails. 由于该类的默认副本构造函数调用了std :: vector的副本构造函数,而后者又调用了std :: unique_ptr的默认构造函数,后来被删除,编译失败。
std::vector<std::unique_ptr<int> >
compiles fine as a local variable in a function. std::vector<std::unique_ptr<int> >
可以作为函数中的局部变量很好地编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.