Construction of a void Type?

前端 未结 5 1225
青春惊慌失措
青春惊慌失措 2020-12-14 17:13

I was given a piece of code that uses void() as an argument. The code doesn\'t compile... obviously?

Can we instantiate anything of type void

5条回答
  •  春和景丽
    2020-12-14 17:55

    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:

    • 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(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.

提交回复
热议问题