問題描述
這是一個非常簡單的問題.考慮以下代碼:
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
工作得很好.但是,使用 Microsoft 編譯器(VS 2010 或 2013)進(jìn)行編譯時出現(xiàn)此錯誤:
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.0VCINCLUDEmemory(2067) : error C2070: 'void': illegal sizeof operand
C:Program Files (x86)Microsoft Visual Studio 10.0VCINCLUDEmemory(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.0VCINCLUDE ype_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.0VCINCLUDEmemory(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
]
這是預(yù)期的嗎?我正在寫一個類,我想在類中擁有一個唯一的指針.在嘗試為類計算移動構(gòu)造函數(shù)的語義時,我遇到了這個問題(我假設(shè)是因?yàn)槲易罱K正確編碼了移動構(gòu)造函數(shù):即其他錯誤已修復(fù)).
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 是錯的:
MSVC is right while GCC is wrong:
標(biāo)準(zhǔn)(3.9/5):
不完全定義的對象類型和void類型是不完全類型
Incompletely-defined object types and the void types are incomplete types
標(biāo)準(zhǔn)(20.7.1.1.2/4):
Standard(20.7.1.1.2/4):
如果 T 是不完整類型,則程序格式錯誤
If T is an incomplete type, the program is ill-formed
這篇關(guān)于應(yīng)該 std::unique_ptr<void>被允許的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!