c++ - vector<unique_ptr> 带来麻烦

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

我的短代码如下:

#include <memory>
#include <vector>
#include <tuple>
using namespace std;

struct A
{
    A() {}
    vector<unique_ptr<int>> m; 
    // Change the above line to "unique_ptr<int> m;" removes the compilation error
    // Or add a line "A(A const&) = delete;" removes the compilation error also
};

struct B
{
    tuple<A> t;
};

int main()
{
    A a;
    B b;
    return 0;
}

VC2013 NOV CTP编译报错:

error C2280: 'std::unique_ptr<int,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function xmemory0   593

这是编译器错误还是代码错误?

最佳答案

这是 Visual Studio 中的一个错误;根据@Gonmator 的评论,它已从 VS2013 更新 1 开始修复。

它可能类似于报告的错误,例如
https://connect.microsoft.com/VisualStudio/feedback/details/801826/std-tuple-with-rvalue-references-not-working-if-clr-enabled
或者
https://connect.microsoft.com/VisualStudio/feedback/details/891428/c-std-pair-has-no-default-move-constructor
— 显然 MSVC 有几个涉及“移动语义 + std::tuple”的残留错误。

关于c++ - vector<unique_ptr> 带来麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22951295/

相关文章:

c++ - 使用 constexpr yield 递减吗?

c++ - 意外的无限循环 (C++)

c++ - 嵌套结构初始化

c++ - 模板参数无效(唯一指针)

c++ - 使用 std::unique_ptr 的递归函数内存泄漏

c++ - 段错误 C++ Cuda

c++ - 有没有更好的方法来设计这个消息传递代码?

c++ - 为什么 MinGW 4.9 显示缺少符合标准的正确代码的初始化程序警告

C++11 lambda 可以访问我的私有(private)成员。为什么?

c++ - 不可移动的 C++17 唯一指针