2

I am seeing following syntax:

auto anonym1 = [] (float a, float b) { return max(a, b); };
auto anonym2 = [] (float a, float b) { return min(a, b); };
auto anonymSel = x > y ? +anonym1 : +anonym2;

If I have written above anonymSel as below, it also compiles with clang c++14.

auto anonymSel = x > y ? anonym1 : anonym2;

Note: x and y are float variables.

Code comment says, for Visual C++ compilation '+' is added. I want to understand what this '+' is doing here.

Tanmay
  • 691
  • 1
  • 7
  • 15
  • 1
    Long story short, the two operands to the conditional operator need to have compatible types, so the author has used `+` to "promote" them from their [distinct] lambda types, into [compatible] function pointer types. Details per the duplicate. – Lightness Races in Orbit Mar 20 '18 at 23:13
  • If that's all the code comment says, though, then that's a bug: the comment should be improved to _really_ explain what the unary `+` is for. – Lightness Races in Orbit Mar 20 '18 at 23:14

0 Answers0