Consider the following code:
void foo() { return void(); }
void bar() { return void{}; }
foo()
compiles, but bar()
doesn't (using GCC 8.2 and clang 7.0 on GodBolt).
Why?
Consider the following code:
void foo() { return void(); }
void bar() { return void{}; }
foo()
compiles, but bar()
doesn't (using GCC 8.2 and clang 7.0 on GodBolt).
Why?
void
is an incomplete type. Since it is an incomplete type the expression void{}
is illegal.
void()
however is granted an exception in [expr.type.conv]/2
[...] If the type is cv void and the initializer is (), the expression is a prvalue of the specified type that performs no initialization. [...]
Which can be/is useful in generic code.
There is an active issue on this and the current proposed wording for the C++20 draft is
[...] Otherwise, if the type is cv void and the initializer is () or {} (after pack expansion, if any), the expression is a prvalue of the specified type that performs no initialization. [...]
Which will allow you to do return void{};
if it is accepted.