gpt4 book ai didi

c++ - 是否应允许 std::unique_ptr

转载 作者:IT老高 更新时间:2023-10-28 12:59:12 28 4
gpt4 key购买 nike

这是一个非常简单的问题。考虑以下代码:

#include <iostream>
#include <memory>

typedef std::unique_ptr<void> UniqueVoidPtr;

int main() {
UniqueVoidPtr p(new int);
return 0;
}

使用以下命令g++ -std=c++0x -o prog file.cpp 用cygwin (g++ 4.5.3) 编译就可以了。但是,使用 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/19840937/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com