How is "int main(){(([](){})());}" valid C++?

Mysticial

I recently came across the following esoteric piece of code.

int main(){(([](){})());}

Reformat it as follows to make it more readable:

int main(){
    (([](){})());   //  Um... what?!?!
}

But I can't get my head around how (([](){})()) is valid code.

  • It doesn't look like function pointer syntax.
  • It can't be some operator overloading trick. The code compiles as is.

Google didn't help much with this all-symbol search. But it compiles in Visual Studio 2010 and outputs nothing. There were no errors, and no warnings. So it looks like valid code.

I've never seen any valid code that is so bizarre outside of Javascript and C function pointers.

Can someone explain how this is valid C++?

Xeo

The code essentially calls an empty lambda.

Let's start from the beginning: [](){} is an empty lambda expression.

Then, in C and C++, you can wrap expressions in parens and they behave exactly the same as if written without them, so that's what the first pair of parens around the lambda does. We're now at ([](){}).

Then, () after the first wrapping parens calls the (empty) lambda. We're now at ([](){})()

The whole expression is wrapped in parens again and we get (([](){})()).

At last, ; ends the statement. We arrive at (([](){})());.


† There are some corner cases at least in C++, like with T a_var; there's a difference between decltype(a_var) and decltype((a_var)).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Is ‘int main;’ a valid C/C++ program?

From Dev

Is int main() { } (without "void") valid and portable in ISO C?

From Dev

How is `int main(int argc, char* argv<::>)` a valid signature of main?

From Java

Does int main() need a declaration on C++?

From Dev

Implement a recursive function in int main() [c++]

From Dev

Cant call int main to repeat program c++

From Dev

Using int main(int argc, char **argv) in c++

From Dev

int main(int argc, char **argv ) with < stdin > stdout in c

From Dev

How to check if an alignment is valid in C?

From Dev

How do I return an array of int to a main method

From Dev

How to check for valid object in Objective-C?

From Dev

How is a C# class with no implementation valid?

From Dev

How to check for valid object in Objective-C?

From Dev

How to return a valid iterator in C++?

From Dev

How is a C# class with no implementation valid?

From Dev

How to check "Is Valid Image File" in C#

From Dev

C++ int main (int argc, char *argv[]) - is argv a c style array?

From Dev

How to stop the int main(void) printf statements when there is an else statement in another function that gives another result?

From Dev

How to validate valid screen resolution (in C/C++)

From Dev

Int main inside a class

From Java

How is “for(const [[[[[a, b, c]]]]] in [0, 0]);” even valid?

From Dev

How to make sure "this" remains valid when using it in C callbacks?

From Dev

How to check if a pointer still points to valid memory in C++?

From Dev

How to ensure the Maze always has a valid path C++

From Dev

How to check a specified string is a valid URL or not using C++ code

From Dev

How to check if a pointer still points to valid memory in C++?

From Dev

How to debug "Cross-thread operation not valid" error in C#?

From Dev

How to check a specified string is a valid URL or not using C++ code

From Dev

How do I Unit test is URL valid in c#?

Related Related

  1. 1

    Is ‘int main;’ a valid C/C++ program?

  2. 2

    Is int main() { } (without "void") valid and portable in ISO C?

  3. 3

    How is `int main(int argc, char* argv<::>)` a valid signature of main?

  4. 4

    Does int main() need a declaration on C++?

  5. 5

    Implement a recursive function in int main() [c++]

  6. 6

    Cant call int main to repeat program c++

  7. 7

    Using int main(int argc, char **argv) in c++

  8. 8

    int main(int argc, char **argv ) with < stdin > stdout in c

  9. 9

    How to check if an alignment is valid in C?

  10. 10

    How do I return an array of int to a main method

  11. 11

    How to check for valid object in Objective-C?

  12. 12

    How is a C# class with no implementation valid?

  13. 13

    How to check for valid object in Objective-C?

  14. 14

    How to return a valid iterator in C++?

  15. 15

    How is a C# class with no implementation valid?

  16. 16

    How to check "Is Valid Image File" in C#

  17. 17

    C++ int main (int argc, char *argv[]) - is argv a c style array?

  18. 18

    How to stop the int main(void) printf statements when there is an else statement in another function that gives another result?

  19. 19

    How to validate valid screen resolution (in C/C++)

  20. 20

    Int main inside a class

  21. 21

    How is “for(const [[[[[a, b, c]]]]] in [0, 0]);” even valid?

  22. 22

    How to make sure "this" remains valid when using it in C callbacks?

  23. 23

    How to check if a pointer still points to valid memory in C++?

  24. 24

    How to ensure the Maze always has a valid path C++

  25. 25

    How to check a specified string is a valid URL or not using C++ code

  26. 26

    How to check if a pointer still points to valid memory in C++?

  27. 27

    How to debug "Cross-thread operation not valid" error in C#?

  28. 28

    How to check a specified string is a valid URL or not using C++ code

  29. 29

    How do I Unit test is URL valid in c#?

HotTag

Archive