Syntactically void()
is an explict type conversion written in functional notation (see 5.2.3).
Note that even in "classic" C (C89/90) explicit conversions to void
were already allowed. Of course, in C one has to use "classic" C-style cast notation and supply an argument. Any expression can be cast to void
in C, including expressions that are already void
. This functionality migrated unchanged to cast notation of explicit type conversion in C++ (it is handled by static_cast
branch of its functionality, i.e. you can static_cast
to void
in C++).
Taking the above into account, it is not surprising that conversion to void
is also consistently supported by the alternative C++ cast syntax - functional notation. And once you understand that, it is no surprise that it was extended to support the "argument-less" version - void()
.
The usefulness of this in C++ will include such contexts as generic functions
template <typename T>
T foo() {
...;
return T(); // remains valid even when `T == void`
}
Note that in C++ it is perfectly legal to return void
pseudo-values from void
functions. The ability to create such void()
pseudo-values eliminates the need to write dedicated void
specializations for functions like the one above.
As a side note, void()
stands for explicit type conversion only if the context forces it to be interpreted as an expression, as in your example. When context calls for a type name (e.g. int foo(void())
or using T = void();
) it actually declares a nameless function that returns void
and takes no arguments.