#include <iostream> void IsTrue(const bool value) { if (value) { std::cout << "value is True!\n"; } } int main() { IsTrue([]() { ; /* some lambda */ }); return 0; }
Output:
value is True!
Why does the lambda evaluate to true
on GCC & Clang? MSVC cannot build this (cannot convert lambda to bool).
Is it a compiler bug? Or which paragraph of the standard allows this?
The C++14 standard (§5.1.2) says:
The closure type for a non-generic lambda-expression with no lambda-capture has a public non-virtual non-explicit const conversion function to pointer to function with C++ language linkage (7.5) having the same parameter and return types as the closure type’s function call operator. The value returned by this conversion function shall be the address of a function that, when invoked, has the same effect as invoking the closure type’s function call operator.
Since a function pointer is implicitly convertible to bool
, you get the result you have shown. This is perfectly legal.
MSVC doesn't compile this because this conversion operator is overloaded with different calling conventions (__stdcall
, __cdecl
, etc). When compiling for x64
all those calling conventions are not used, so there's just one conversion operator and it compiles fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With