vc++2010/2012:std::包含unique_ptr编译器错误的结构的向量

vc++ 2010/2012: std::vector of a struct containing unique_ptr compiler error

本文关键字:错误 编译器 ptr 结构 向量 unique 2012 std 包含 vc++2010      更新时间:2023-10-16

以下代码生成下面的编译器错误(在代码之后),但如果矢量直接包含unique_ptr,则不会生成(请参见注释的代码行)。有什么想法吗?

这个问题更关心的是"#if1"块中的代码块,"#else"块生成类似但更令人期待的错误(在将"#if 1"更改为"#if 0"之后)。

// MoveSemantics.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <memory>
#include <vector>
typedef std::unique_ptr<int> upi;
struct S
{
    S() : p(new int(123)) {}
    S(S&& s) : p( std::move(s.p) ) {} // NB: the move constructor is supposed to be used? (but not)
    upi p;
};
#if 1
void test()
{
    //std::vector<S> vs; // Okay
    //std::vector<upi> vupi(10); // Okay
    std::vector<S> vs(10); // Error! why in the hell does the compiler want to generate a copy constructor here??
}
#else
void test()
{
    std::vector<S> vs;
    vs.push_back( S() );
    const S& s = vs.front();
    //S& s = vs.front(); // fine!
    S s1 = std::move(s); // Error, but expected
}
#endif
int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

编译器错误:

1> error C2248: 'std::unique_ptr<_Ty>::operator =' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          c:program filesmicrosoft visual studio 11.0vcincludememory(1435) : see declaration of 'std::unique_ptr<_Ty>::operator ='
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          This diagnostic occurred in the compiler generated function 'S &S::operator =(const S &)'

这看起来像std::lib中的一个bug。我确信它之所以出现在这里,是因为vector规范的发展历史。

在C++98/03中,vector有这样的构造函数:

explicit vector(size_type n, const T& value = T(), const Allocator& = Allocator());

规范是T将默认构造一次,然后在后两个参数默认的情况下调用时复制构造的n次。

在C++11中,它被更改为:

explicit vector(size_type n);
vector(size_type n, const T& value, const Allocator& = Allocator());

第二个构造函数的规范没有改变。但第一个做到了:它应该默认构造T n次,而不是复制(或移动)它。

我本以为错误消息会说正在使用unique_ptr的已删除或专用的复制构造函数。这表明vector遵循C++98/03规范,只是还没有更新。

但是,由于诊断程序抱怨unique_ptr复制分配,因此看起来vector已更新,但更新不正确。听起来像是在使用C++98/03:中的签名

explicit vector(size_type n, const T& value = T(), const Allocator& = Allocator());

默认构造nT,然后将value分配给nT

您没有包含作为vector需求一部分的移动赋值运算符,只包含一个移动构造函数。