It turns out that it is standard!
If you refer to this answer[1], non-capturing lambdas are convertible to function pointers. And it turns out again that function pointers, being pointers themselves, are implicitly convertible to bool
!
4.12 Boolean conversions [conv.bool]
1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a
prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false;
any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of
type bool; the resulting value is false.
To give a supporting proof that the conversion to function pointer is what makes all of this happen, I've tried doing the same thing with capturing lambdas. Then "can't convert to bool
" errors are generated.
LIVE CODE
int main() {
int i;
auto lambda = [i]{};
bool b = lambda;
if(lambda) {}
}
[1] Which, honestly, gave me the idea to write this.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…