应该std :: unique_ptr< void>允许 [英] Should std::unique_ptr<void> be permitted
问题描述
这是一个很简单的问题。考虑下面的代码:
This is a very simple question. Consider the following code:
#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
工作正常。但是,使用微软编译器(VS 2010或2013)编译我得到这个错误:
Compiling with cygwin (g++ 4.5.3) with the following command g++ -std=c++0x -o prog file.cpp
works just fine. However, compiling with the microsoft compiler (either VS 2010 or 2013) I get this error:
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
]
这是否预期?我在写一个类,我想在类中有一个唯一的指针。当试图找出类的移动构造函数的语义时,我遇到了这个(我假设因为我最终得到我的移动构造函数正确编码:即其他错误是固定的)。
Is this expected? I'm writing a class where I wanted to have a unique pointer in the in the class. While trying to work out the semantics of a move constructor for the class, I ran into this (I assume because I finally got my move constructor coded correctly: i.e. the other errors were fixed).
推荐答案
MSVC是正确的,而GCC错误:
标准(3.9 / 5):不完全定义的对象类型和void类型不完整的
类型
标准(20.7.1.1.2 / 4):如果T是不完整的类型,程序是错误的
MSVC is right while GCC is wrong:
Standard(3.9/5): Incompletely-defined object types and the void types are incomplete
types
Standard(20.7.1.1.2/4): If T is an incomplete type, the program is ill-formed
这篇关于应该std :: unique_ptr< void>允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!