About this blog

Save duplicate questions from disappearing from Google

Is any type convertible to void?

Question

All of the following expressions compile fine:

void();
void(5);
void("hello");
void(std::string("world"));
void(std::vector<int>{1, 2, 3});

Does this mean that any type can be converted to void? That is void is constructible from an argument of any type.

However the following function doesn't compile:

void func()
{
    return std::vector<int>{1, 2, 3};
}

But if I convert the return value to void explicitly, it compiles:

void func()
{
    return void(std::vector<int>{1, 2, 3});
}

Does it mean that all void constructors are explicit?

The answer

Yes, a value of any type can be explicitly converted to void.

Section 3.9.1/9, N3797:

The void type has an empty set of values. The void type is an incomplete type that cannot be completed. It is used as the return type for functions that do not return a value. Any expression can be EXPLICITLY converted to type cv void (5.4). An expression of type void shall be used only as an expression statement (6.2), as an operand of a comma expression (5.18), as a second or third operand of ?: (5.16), as the operand of typeid, noexcept, or decltype, as the expression in a return statement (6.6.3) for a function with the return type void, or as the operand of an explicit conversion to type cv void.