对持有unique_ptr类型vector的对象列表进行排序

Sorting a list of objects holding a vector of unique_ptr

本文关键字:对象 列表 排序 vector 类型 unique ptr      更新时间:2023-10-16

下面的代码应该根据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; });
}

Visual c++ 2012产生以下编译错误:

1>c:program files (x86)microsoft visual studio 11.0vcincludexmemory0(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.0vcincludememory(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.0vcincludexmemory0(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.0vcincludexmemory0(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.0vcincludetype_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.0vcincludevector(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:test2test2.cpp(213) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=std::unique_ptr<int>
1>          ]

这是" VC的问题",但只是因为你滥用了Visual Studio。

vc++实现了r值引用,但是没有实现编译器生成的移动构造函数/赋值操作符。这意味着,如果你想让一个类型是可移动的,你必须自己写一个。

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

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

这个问题实际上是在VC11中,因为它没有实现c++ 11的自动生成移动操作的功能(已经被nicolbolas确定)。

下面的代码在VC10 SP1下编译;在这个代码示例中,move构造函数是显式地编写的(对于move operator=,使用复制和交换习惯)。

#include <algorithm>  // for std::swap (for copy-and-swap idiom)
#include <list>
#include <memory>
#include <vector>
struct A
{
    std::vector<std::unique_ptr<int>> v;
    A(A&& other)
        : v( std::move(other.v) )
    {
    }
    A& operator=(A other)
    {
        swap(*this, other);
        return *this;
    }
    friend void swap(A& lhs, A& rhs)
    {
        using std::swap;
        swap(lhs.v, rhs.v);
    }
};
int main()
{
    std::list<A> l;
    l.sort( []( const A& , const A& ){ return true; } );
}

这是Visual c++ 2012的一个问题(由Microsoft在Connect上承认:c++代码中的编译错误排序持有unique_ptr向量的对象列表),它已经在Visual c++ 2013中修复了。

另外,我想指出一个问题与visualc++不隐式地生成move构造函数这一事实无关。如果在我的原始示例中显式删除结构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; });
}