-Wpointer-bool-conversion/-Waddress misses an obvious case (original) (raw)

The following pattern is often used to evaluate an expensive function only once, cache its result, and return that on subsequent calls.

int foo() {
    static bool is_true = [](){ return expensive_helper_function; };

    return is_true;
}

But this code is subtly wrong. It should be

static bool is_true = [](){ return expensive_helper_function; }(); // Note the parens!

In the bad case it looks as though the lambda is being evaluated to a pointer(?) and this is being evaluated always to true when it is assigned to is_true.

It feels as though -Wpointer-bool-conversion should flag this, but it does not (godbolt).