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
You can use void() as a callable type, as an example std::function<void()> f; is a valid statement.
Moreover, as from 8.3.5/4:
A parameter list consisting of a single unnamed parameter of non-dependent type void is equivalent to an empty parameter list.
That means that this is valid:
template<typename T>
struct F;
template<typename R, typename... A>
struct F<R(A...)> { };
int main () {
F<void(void)> s;
}
Here you are not instantiating anything of type void, but you are using it (let me say) as a parameter list of a callable type.
Not sure if this replies to your question, I've not clear what the question actually is.
There is no place for void() in C++ is there?
As an expression, void() is valid in C++.
From the standard, $5.2.3/2 Explicit type conversion (functional notation) [expr.type.conv]:
The expression
T(), whereTis a simple-type-specifier or typename-specifier for a non-array complete object type or the (possibly cv-qualified)voidtype, creates a prvalue of the specified type, whose value is that produced by value-initializing (8.5) an object of typeT; no initialization is done for thevoid()case.
From cppreference.com:
new_type ( )
If
new_typeis an object type, the object is value-initialized; otherwise, no initialization is done. Ifnew_typeis (possibly cv-qualified)void, the expression is avoidprvalue.
C++ (and I say C++, not C) allows (§6.6.3 comma 2) functions with void return type to return a void expression, that is:
void foo() { return void(); }
But notice it is not constructing a temporary void!
You can take a void() as function parameter:
void test(void()) { ... }
Which expands to:
void test(void (*)())
Which is a function pointer to a method which returns void and takes no arguments.
Full example:
void abc() {}
void test(void()) { }
int main() {
test(abc);
}
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();true ? throw 1 : void()++it1, void(), ++it2decltype or noexcept: using my_void = decltype(void()); static_assert(noexcept(void()), "WAT");return statement of a function returning (possibly cv-qualified) void: const void f() { return void(); }void: static_cast<const void>(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.