How come this code compiles???
LIVE CODE
#include <iostream>
int main() {
auto lambda1 = []{};
auto lambda2 = []{};
if(lambda1 && lambda2) {
std::cout << "BOOLEAN LAMBDAS!!!" << std::endl;
}
if(lambda1 || lambda2) {
std::cout << "BOOLEAN LAMBDAS AGAIN FTW!!!" << std::endl;
}
bool b1 = lambda1;
bool b2 = lambda2;
std::cout << b1 << ", " << b2 << std::endl;
}
Boolean lambdas! (Or boolambdas, if you will... *shies away*)
How come this works? Is this another GCC bug? If not, is this standard?
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.