Here is my code:
int main(int argc, char** argv) {
bool gg;
if( [&]()->decltype(gg){
return false; //try changing this to true or false and you'll get the same result.
} ){
std::cout<<"all even"<<std::endl;
}else {
std::cout<<"all odd"<<std::endl;
}
return 0;
}
It's just simple, i have an if else statement and a lambda function inside it that checks the condition. I don't know if it's the code or compiler but even if i change false to true and vice versa, i get the same result. I am using Dev CPP. What's wrong with my code?
That is exactly what happens in your case. You forgot to invoke your closure object's
()
operator. Instead you used the closure object itself as a condition inif
.Since your closure object does not capture anything, per 5.1.2/6 it is implicitly convertible to plain function pointer type
bool (*)()
. And so your object got implicitly converted to a function pointer. Since the pointer is not null, it acts astrue
underif
.On other words, your code is interpreted by the compiler in the following way
If you make your lambda function actually capture something (e.g. replace
return false;
withreturn gg;
), your original code will immediately fail to compile.P.S. As you can read in the comments below, the very presence of
[&]
apparently should have disabled 5.1.2/6 for your lambda. But apparently your compiler treats 5.1.2/6 more loosely and looks for actual captures instead of simply checking for presence of non-[]
capture clause.You don't evaluate the lambda, you just check the object:
edit: Ideone code https://ideone.com/yxloQf