Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对包含唯一\u ptr向量的对象列表进行排序_C++_List_C++11_Vector_Unique Ptr - Fatal编程技术网

C++ 对包含唯一\u ptr向量的对象列表进行排序

C++ 对包含唯一\u ptr向量的对象列表进行排序,c++,list,c++11,vector,unique-ptr,C++,List,C++11,Vector,Unique Ptr,根据C++11,下面的代码应该会产生编译错误(如果是,为什么?),还是VC11有问题 #include <vector> #include <list> #include <memory> struct A { std::vector<std::unique_ptr<int>> v; }; int main() { std::list<A> l; l.sort([](const A& a1,

根据C++11,下面的代码应该会产生编译错误(如果是,为什么?),还是VC11有问题

#include <vector>
#include <list>
#include <memory>
struct A
{
    std::vector<std::unique_ptr<int>> v;
};
int main()
{
    std::list<A> l;
    l.sort([](const A& a1, const A& a2){ return true; });
}
#包括
#包括
#包括
结构A
{
std::向量v;
};
int main()
{
std::列表l;
l、 排序([](常量A和a1,常量A和a2){return true;});
}

VisualC++ 2012产生以下编译错误:

1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(606): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(1447) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(605) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)'
1>          with
1>          [
1>              _Ty=std::unique_ptr<int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(751) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' being compiled
1>          with
1>          [
1>              _Ty=std::unique_ptr<int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\type_traits(743) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=std::unique_ptr<int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=std::allocator<std::unique_ptr<int>>
1>          ]
1>          d:\test2\test2.cpp(213) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=std::unique_ptr<int>
1>          ]
1>c:\program files(x86)\microsoft visual studio 11.0\vc\include\xmemory0(606):错误C2248:“std::unique\u ptr::unique\u ptr”:无法访问类“std::unique\u ptr”中声明的私有成员
1> 与
1>          [
1> _Ty=int
1>          ]
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\memory(1447):请参阅“std::unique\u ptr::unique\u ptr”的声明
1> 与
1>          [
1> _Ty=int
1>          ]
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xmemory0(605):编译类模板成员函数“void std::allocator::construct(_Ty*,const _Ty&)”时
1> 与
1>          [
1> _Ty=std::unique_ptr
1>          ]
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\xmemory0(751):请参阅正在编译的函数模板实例化“void std::allocator::construct(_Ty*,const _Ty&)”的参考
1> 与
1>          [
1> _Ty=std::unique_ptr
1>          ]
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\type\u traits(743):请参阅正在编译的类模板实例化“std::allocator”的参考
1> 与
1>          [
1> _Ty=std::unique_ptr
1>          ]
1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\vector(655):请参阅正在编译的类模板实例化“std::is_empty”的参考
1> 与
1>          [
1> _Ty=std::分配器
1>          ]
1> d:\test2\test2.cpp(213):请参阅对正在编译的类模板实例化“std::vector”的引用
1> 与
1>          [
1> _Ty=std::unique_ptr
1>          ]
这是“VC的一个问题”,但这只是因为您误用了Visual Studio

VC++实现r值引用,但不实现编译器生成的移动构造函数/赋值运算符。这意味着,如果你想要一个类型是可移动的,你必须自己写一个

A
不是可移动类型,因此各种
std::list
函数将尝试复制它们。当他们试图复制
唯一的
向量时,就会失败。因此出现了编译器错误


如果你想在VC++中使用移动感知对象,你必须自己为它们编写移动构造函数/赋值。

问题实际上在VC11中,正如(Nicol Bolas已经指出的那样)

以下代码使用VC10 SP1编译;在这个代码示例中,move构造函数是显式编写的(对于move
operator=
,使用的是)

#include//for std::swap(用于复制和交换习惯用法)
#包括
#包括
#包括
结构A
{
std::向量v;
A(A和其他)
:v(标准::移动(其他.v))
{
}
A运算符=(A其他运算符)(&O)
{
掉期(*本,其他);
归还*这个;
}
朋友无效掉期(A&lhs、A&rhs)
{
使用std::swap;
交换(左、右、右);
}
};
int main()
{
std::列表l;
l、 排序([](常量A&,常量A&){return true;});
}

> p>这是Visual C++ 2012(微软在Connect上承认)的问题,它已经在Visual C++ 2013中被固定。

也要指出,一个问题与事实无关,VisualC++没有隐含地生成移动构造函数。在我的原始示例中,如果显式删除结构A中的所有复制和移动构造函数(是的,这将使无法将类型A的对象插入列表,但这不是重点),则代码仍然不应复制或移动任何对象,因此会产生编译错误:

#include <vector>
#include <list>
#include <memory>
struct A
{
    std::vector<std::unique_ptr<int>> v;
    A(A&&) = delete;
    A(const A&) = delete;
};
int main()
{
    std::list<A> l;
    l.sort([](const A& a1, const A& a2){ return true; });
}
#包括
#包括
#包括
结构A
{
std::向量v;
A(A&&)=删除;
A(常数A&)=删除;
};
int main()
{
std::列表l;
l、 排序([](常量A和a1,常量A和a2){return true;});
}

作为参考,我可以在Clang和GCC中很好地编译它。所以这要么是你的编译器,要么是你的设置。@chrisaycock哦,好吧,要在Microsoft Connect上创建另一个VC11错误报告……我宁愿完全放弃它。Microsoft已经发布了两个vs2012更新补丁。你都安装了吗?@rohitsan当然,我正在使用更新2。为什么排序列表(而不是向量)需要复制任何内容?排序不是应该通过改变双链表节点的“上一个”和“下一个”指针来实现吗?我很清楚什么是移动构造函数,以及VC11不会隐式生成它们的事实。这丝毫不能解释为什么VC11在执行列表排序时要复制或移动A类型的对象。那么,您可以在STL源代码中找到答案。我明白你的意思。
#include <vector>
#include <list>
#include <memory>
struct A
{
    std::vector<std::unique_ptr<int>> v;
    A(A&&) = delete;
    A(const A&) = delete;
};
int main()
{
    std::list<A> l;
    l.sort([](const A& a1, const A& a2){ return true; });
}