7

Is the following code supposed to produce compilation error according to C++11 (if so why?) or is it a problem with 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 produces the following compilation error:

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>          ]
4

3 回答 3

4

这是“VC 的问题”,但这仅仅是因为您滥用了 Visual Studio。

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

A不是可移动类型,因此各种std::list函数将尝试复制它们。当他们试图复制vector. unique_ptr因此编译器错误。

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

于 2013-05-05T15:26:01.203 回答
3

The problem is really in VC11, as it doesn't implement C++11 feature of automatically generating move operations (as already nailed by Nicol Bolas).

The following code compiles with VC10 SP1; in this code sample, move constructor is explicitly written (instead for move operator=, the copy-and-swap idiom is used).

#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; } );
}
于 2013-05-05T21:48:03.083 回答
1

这是 Visual C++ 2012 的一个问题(Microsoft 在 Connect 上承认:C++ 代码中的编译错误对包含 unique_ptr 向量的对象列表进行排序),并且它已经在 Visual C++ 2013 中得到修复。

另外,我想指出一个问题与事实无关,即 Visual C++ 不会隐式生成移动构造函数。如果您在我的原始示例中显式删除结构 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; });
}
于 2014-07-26T11:44:21.103 回答