vc++2010/2012:std::包含unique_ptr编译器错误的结构的向量
vc++ 2010/2012: std::vector of a struct containing unique_ptr compiler error
以下代码生成下面的编译器错误(在代码之后),但如果矢量直接包含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());
默认构造n
的T
,然后将value
分配给n
的T
。
您没有包含作为vector
需求一部分的移动赋值运算符,只包含一个移动构造函数。
相关文章:
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 有没有办法让编译器在我放置字符串而不是 nlohmann::json 对象时抛出错误?
- 致命错误 C1001 :vs2017 15.8.4 的内部错误(编译器文件"msc1.cpp",第
- C++2440错误-编译器认为字符串是常量字符
- Clang 错误 – 编译器错误或缺少一些细节?
- 视觉C++ 2017 错误?编译器优化表达式
- 配置:错误:C++编译器无法创建可执行文件
- PHP PDO_ODBC配置:错误:C 编译器无法创建可执行文件
- 致命错误 C1001:内部编译器错误(编译器文件"MSC1.cpp",第 1794 行)
- 错误:编译器限制达到简单hello world程序的堆限制
- c++链接错误:编译器找不到函数的定义
- 生成 CImg 时出现致命错误(编译器限制)
- 抛出对齐类型时出现 Clang 运行时错误.编译器错误