mardi 23 janvier 2018

Initializing capturing lambda in ternary operator

I wanted to create a lambda in the following way:

auto l1 = condition ?
          [](){ return true; } :
          [number](){ return number == 123; };

However, I got error:

operands to ?: have different types ‘main()::<lambda()>’ and ‘main()::<lambda()>’

Obviously, the types seem to be the same. I thought, that capturing number in only one of lambdas might be a problem, but I get the same error for these:

auto l2 = condition ?
          [number](){ return true; } :
          [number](){ return number == 123; };

auto l3 = condition ?
          [number](){ return number != 123; } :
          [number](){ return number == 123; };

I'm aware I can do it other way (below), but I'm wondering what is the cause of this behavior. It was compiled with GCC6.3.1, C++14 enabled.

//compiles      
auto l4 = condition ?
          [](const int){ return true; } :
          [](const int number){ return number == 123; };

Aucun commentaire:

Enregistrer un commentaire