1

I tried printing the smiley-with-beard lambda expression

#include <iostream>

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

but it printed

1

instead. How?

2
  • 1
    put <:]{%> in quotes like "<:]{%>"
    – R Nar
    Oct 6, 2015 at 18:41
  • 2
    Probably it triggers an implicit conversion to a non-null pointer to function and is printed as a boolean Oct 6, 2015 at 18:42

2 Answers 2

6

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.

2

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

You should have :

std::cout << "<:]{%>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.