给了我一段使用 void()
的代码作为论据。代码无法编译……显然?
我们可以实例化 void
类型的任何东西吗? ?我相信答案是否定的,除了 void*
.例如:
- 编写函数
void askVoid(void param) {}
错误:
A parameter may not have
void
type
- 编写函数
void askNaught() {}
并用 askNaught(void())` 错误调用它:
error C2660:
takeNaught
: function does not take 1 arguments
- 编写模板化函数
template <typename T> void takeGeneric(T param) {}
并用takeGeneric(void())
调用它错误:
error C2893: Failed to specialize function template
void takeGeneric(T)
- 声明
void voidType
错误:
Incomplete type is not allowed
- 声明
auto autoVoid = void()
错误:
Cannot deduce
auto
type
- 声明
void* voidPtr
工作正常,但remove_pointer_t<decltype(voidPtr)> decltypeVoid
错误:
error C2182:
decltypeVoid
: illegal use of typevoid
就是这样,对吧?没有void()
的地方在 C++ 中有吗?这只是我得到的错误代码,对吧?
最佳答案
C++(我说的是 C++,而不是 C)允许 (§6.6.3 comma 2 ) 具有 void
返回类型的函数返回一个 void
表达式,即:
void foo() { return void(); }
但请注意,它不是构造一个临时的void
!
关于c++ - 构造一个 void 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37701321/