The C++11 rules for return types create a bit of an interesting case:
A return statement with an expression of non-void type can be used only in functions returning a value; the value of the expression is returned to the caller of the function. The value of the expression is implicitly converted to the return type of the function in which it appears.
and later
A return statement with an expression of type void
can be used only in functions with a return type of cv
void
; the expression is evaluated just before the function returns to its caller.
It seemed natural to create a type that would implicitly convert to void
. Unfortunately the expression has this custom type, which counts as non-void type, and is rejected.
struct does_not_work
{
operator void() const {}
template<typename T> operator T() const { return T{}; }
};
I'm not sure whether I would support rewording the rule to allow types that implicitly convert to the return type even if that return type is void.
Similarly, this is rejected:
return {};
(but so is void{}
)