c++ - 应该允许 std::unique_ptr<void>

标签 c++ visual-studio-2010 gcc c++11 unique-ptr

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

#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/

相关文章:

c++ - 如何使用 GCC 4.8 配置 libstdc++?

android - OpenGL ES 在不使用 glPixelStorei 的情况下更新纹理的子部分

c++ - C/C++ : how to separate addressing mode code from actual instruction code 中的 6502 仿真器

c# - WP7,将服务引用添加到 Visual Studio 2010 后如何使用它

c++ - 未捕获 vector 的异常

c - 使用自定义目录和库的 makefile

c++ - 混合库类型(静态与动态)有问题

c++ - LLU 号码后缀错误

c++ - CMake错误: Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS) using clang on Mac OS

visual-studio-2010 - 如何更新 Visual Studio 的 "Cascading Style Sheet Version for Validation"