构造一个空类型? [英] Construction of a void Type?
问题描述
我得到了一段使用 void()
作为参数的代码。代码显然无法编译...
I was given a piece of code that uses void()
as an argument. The code doesn't compile... obviously?
我们可以实例化 void
类型的任何东西吗?我相信答案是否定的,除了 void *
以外。例如:
Can we instantiate anything of type void
? I believed the answer was no, with the exception of a void*
. For example:
- 编写函数
void askVoid(void param){}
错误:
- Writing the function
void askVoid(void param) {}
errors:
参数可能没有
void
类型
- 编写函数
void askNaught(){ }
并使用askNaught(void())`错误进行调用:
- Writing the function
void askNaught() {}
and calling it with askNaught(void())` errors:
错误C2660:
takeNaught
:函数不接受1个参数
error C2660:
takeNaught
: function does not take 1 arguments
- 编写模板函数
template< typename T> void takeGeneric(T param){}
并以takeGeneric(void())
错误进行调用:
- Writing the templatized function
template <typename T> void takeGeneric(T param) {}
and calling it withtakeGeneric(void())
errors:
错误C2893:未能专门化功能模板
void takeGeneric(T)
- 声明
void voidType
错误:
- Declaring
void voidType
errors:
不允许使用不完整的类型
Incomplete type is not allowed
- 声明
auto autoVoid = void()
错误:
- Declaring
auto autoVoid = void()
errors:
无法推断
auto
类型
- 声明
void * voidPtr
可以,但是remove_pointer_t< decltype(voidPtr)> decltypeVoid
错误:
- Declaring
void* voidPtr
works fine, butremove_pointer_t<decltype(voidPtr)> decltypeVoid
errors:
错误C2182:
decltypeVoid
:非法使用void
对的?在C ++中没有 void()
的地方吗?
That's it, right? There is no place for void()
in C++ is there? This is just bad code I've been given, right?
推荐答案
表达式 void()
是 void
类型的prvalue,可以在可能使用此类表达式的任何地方使用,即[basic.fundamental]/9 有助于提供以下列表:
The expression void()
is a prvalue of type void
and can be used anywhere such an expression may be used, which [basic.fundamental]/9 helpfully provides a list:
- 作为表达式语句:
void();
- 作为条件运算符的第二或第三操作数:
是吗?抛出1:void()
- 作为逗号运算符的操作数:
++ it1,void(),++ it2
- 作为
decltype
或noexcept
的操作数:使用my_void = decltype(void()); static_assert(noexcept(void()( WAT), WAT);
- 在
return
语句中返回(可能具有CV资格)void
的函数:const void f(){return void(); }
- 作为显式转换为(可能具有cv资格的)
void
的操作数:static_cast< const void>(void())
- As an expression-statement:
void();
- As the second or third operand of a conditional operator:
true ? throw 1 : void()
- As an operand of the comma operator:
++it1, void(), ++it2
- As the operand of
decltype
ornoexcept
:using my_void = decltype(void()); static_assert(noexcept(void()), "WAT");
- In a
return
statement of a function returning (possibly cv-qualified)void
:const void f() { return void(); }
- As an operand of an explicit conversion to (possibly cv-qualified)
void
:static_cast<const void>(void())
类型为 void
也可以用作 typeid
的操作数,但 void()$ c $在这种情况下,c>特别是将被解析为类型,而不是表达式。
An expression of type void
can also be used as the operand of typeid
, but void()
in particular would be parsed as a type, not an expression, in this context.
这篇关于构造一个空类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!