这是一个非常简单的问题。考虑以下代码:
#include <iostream>
#include <memory>
typedef std::unique_ptr<void> UniqueVoidPtr;
int main() {
UniqueVoidPtr p(new int);
return 0;
}
使用以下命令编译 cygwin (g++ 4.5.3) g++ -std=c++0x -o prog file.cpp
工作正常。但是,使用 Microsoft 编译器(VS 2010 或 2013)进行编译时出现此错误:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\memory(2067) : error C2070: 'void': illegal sizeof operand
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\memory(2066) : while compiling class template member function 'void std::default_delete<_Ty>::operator ()(_Ty *) const'
with
[
_Ty=void
]
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\type_traits(650) : see reference to class template instantiation 'std::default_delete<_Ty>' being compiled
with
[
_Ty=void
]
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\memory(2193) : see reference to class template instantiation 'std::tr1::is_empty<_Ty>' being compiled
with
[
_Ty=std::default_delete<void>
]
foo1.cpp(7) : see reference to class template instantiation 'std::unique_ptr<_Ty>' being compiled
with
[
_Ty=void
]
这是预期的吗?我正在写一个类,我想在类中有一个唯一的指针。在尝试为该类计算移动构造函数的语义时,我遇到了这个问题(我假设是因为我终于正确地编码了我的移动构造函数:即其他错误已修复)。
最佳答案
MSVC 是对的,GCC 是错的:
标准(3.9/5):
Incompletely-defined object types and the void types are incomplete types
标准(20.7.1.1.2/4):
If T is an incomplete type, the program is ill-formed
关于c++ - 应该允许 std::unique_ptr<void>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35098466/