Should std::unique_ptrlt;voidgt; be permitted(应该 std::unique_ptrvoid被允许)
问题描述
这是一个非常简单的问题.考虑以下代码:
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)进行编译时出现此错误:
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
]
这是预期的吗?我正在写一个类,我想在类中拥有一个唯一的指针.在尝试为类计算移动构造函数的语义时,我遇到了这个问题(我假设是因为我最终正确编码了移动构造函数:即其他错误已修复).
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:
标准(3.9/5):
不完全定义的对象类型和void类型是不完全类型
Incompletely-defined object types and the void types are incomplete types
标准(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
这篇关于应该 std::unique_ptr<void>被允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:应该 std::unique_ptr<void>被允许
- 从python回调到c++的选项 2022-11-16
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 近似搜索的工作原理 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01