0

I tried printing the smiley-with-beard lambda expression

#include <iostream>

int main() {
    std::cout << <:]{%>; // smile!
    return 0;
}

but it printed

1

instead. How?

Community
  • 1
  • 1
abcoep
  • 577
  • 9
  • 14

2 Answers2

5

As explained in the answers to the question you've linked to,

<:]{%>

is equivalent to

[]{}

A lambda expression that doesn't capture anything is implicitly convertible to a function pointer. In this case, the signature of this function pointer is void(*)().

Now, the function pointer is implicitly convertible to a boolean value which is always true, hence the output prints 1.

Community
  • 1
  • 1
Praetorian
  • 106,671
  • 19
  • 240
  • 328
2

You forgot to put " around <:]{%>.

You should have :

std::cout << "<:]{%>";
N. Hereman
  • 33
  • 6