简体   繁体   English

std :: vector <std::unique_ptr<int> &gt;不编译

[英]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.

相关问题 std::向量<std::unique_ptr<unimplementtype> > 编译错误</std::unique_ptr<unimplementtype> - std::vector<std::unique_ptr<UnimplementType>> compile error 如果没有 noexcept 移动构造函数,为什么带有 std::vector 的代码不能编译,但带有 std::unique_ptr 的代码却可以编译? - Why does code with std::vector not compile but with std::unique_ptr it does, if there is no noexcept move constructor? 使用std :: unique_ptr的std :: vector - using an std::vector of std::unique_ptr 分配std :: vector <std::unique_ptr<T> &gt;到另一个std :: vector <std::unique_ptr<T> &gt; - Assign std::vector<std::unique_ptr<T>> to another std::vector<std::unique_ptr<T>> 转换向量<std::unique_ptr<A> &gt; 到矢量 - Transforming a vector<std::unique_ptr<A>> to vector<A> 如何调整 std::vector 的大小<std::queue<std::unique_ptr<int> &gt;&gt;? </std::queue<std::unique_ptr<int> - How to resize a std::vector<std::queue<std::unique_ptr<int>>>? 为什么std :: shared_ptr <T> = std :: unique_ptr <T[]> 编译,而std :: shared_ptr <T[]> = std :: unique_ptr <T[]> 才不是? - Why does std::shared_ptr<T> = std::unique_ptr<T[]> compile, while std::shared_ptr<T[]> = std::unique_ptr<T[]> does not? 用unique_ptr初始化std :: vector - Initializing std::vector with unique_ptr 根据pair.first排序std :: pair <int,std :: unique_ptr <const T>>的向量 - sorting a vector of std::pair<int, std::unique_ptr<const T> > depending on pair.first 将std :: vector &lt;std :: unique_ptr &lt;int &gt;&gt;的所有权转移到正在构造的类的正确方法 - Proper way of transferring ownership of a std::vector< std::unique_ptr< int> > to a class being constructed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM