Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 views
in Technique[技术] by (71.8m points)

c++ - Boolean lambdas?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...