构造一个空类型? [英] Construction of a void Type?

查看:146
本文介绍了构造一个空类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一段使用 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:


  1. 编写函数 void askVoid(void param){} 错误:

  1. Writing the function void askVoid(void param) {} errors:




参数可能没有 void 类型




  1. 编写函数 void askNaught(){ } 并使用askNaught(void())`错误进行调用:

  1. Writing the function void askNaught() {} and calling it with askNaught(void())` errors:




错误C2660: takeNaught :函数不接受1个参数

error C2660: takeNaught: function does not take 1 arguments




  1. 编写模板函数 template< typename T> void takeGeneric(T param){} 并以 takeGeneric(void())错误进行调用:

  1. Writing the templatized function template <typename T> void takeGeneric(T param) {} and calling it with takeGeneric(void()) errors:




错误C2893:未能专门化功能模板 void takeGeneric(T)




  1. 声明 void voidType 错误:

  1. Declaring void voidType errors:




不允许使用不完整的类型

Incomplete type is not allowed




  1. 声明 auto autoVoid = void()错误:

  1. Declaring auto autoVoid = void() errors:




无法推断 auto 类型




  1. 声明 void * voidPtr 可以,但是 remove_pointer_t< decltype(voidPtr)> decltypeVoid 错误:

  1. Declaring void* voidPtr works fine, but remove_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 or noexcept: 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()特别是将被解析为类型,而不是表达式。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆