How is "int main(){(([](){})());}" valid C++?
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))
.
Related videos on Youtube
Mysticial
Alexander Yee - Formerly at Google in San Bruno, CA. Now I work in financial services. I'm an enthusiast in computer hardware and programming. I specialize in high performance and parallel computing. I build computers, overclock them, and write software to torture the hell out of them. I'm also the author of y-cruncher - a program that computes Pi and other constants. It was used to set the world record for the most digits of Pi ever computed at 5 trillion (August 2010). It currently holds that record at 31.4 trillion digits (January 2019).
Updated on June 09, 2022Comments
-
Mysticial almost 2 years
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++?
-
sehe over 11 yearsHey! That's mine. "
Don't sweat it. We have int main(){(([](){})());} which is valid C++"
(Nov 9th in chat) -
Admin over 11 yearsit's a c++11 lambda closure
-
Ramon Snir over 11 yearsRead about C++ lambdas, this is what
[](){}
is (an empty lambda function). -
Mysticial over 11 years6 years of C++, and I've never learned lambdas... I guess that's next on my to-do list.
-
sehe over 11 yearsAlso: "
@Mysticial int main()??<((??(??)()??<??>)());??>
" - much more convincing -
SChepurin over 11 years@Mysticial - This code mystifies you because it is useless. If this lambda would do something, you would recognize it having syntax similar to function pointers (with which it is related closely).
-
Mysticial over 11 years@sehe My fault for not getting the message. The rest of the room was in favor of posting this trivia question. So I missed your dissenting opinion. I'll give you call on whether or not we should delete this.
-
Pete Becker over 11 years@Mysticial - "6 years of C++" -- lambdas were just added in C++11, so nobody has experience with them before a year or so ago.
-
tckmn about 10 yearsThe URL here is quite amusing: "how-is-int-main-valid-c"
-
Trismegistos almost 10 yearsIs this equation correct {(([ = ](){})());}?
-
Avinash R almost 10 yearsjust for info, this will compile on gcc (using
g++
command). withg++ -std=c++11 file.c
-
guest-418 over 9 yearsint main(){ return !<:]()<%;[=:>(){<:&](){;%>();%>();}; } is still valid. I don't even remember where I have found it(somewhere on so).
-
Fermat's Little Student over 8 yearsI used to think JavaScript is the only esoteric programming language widely used in production...
-
AnArrayOfFunctions over 5 yearsIt is also valid:
int ((main)()){(([](){})());}
but I don't brag about it. -
Admin about 4 yearsNo
-
Xeo over 11 years@R.MartinhoFernandes: It was still stuck in someone, so I had to go and retrieve it.
-
sehe over 11 yearsI was gonna upvote for correctly mentioning the case where adding () around an expression alters the semantics. But then I remembered that has no relation to the question, really. Nice answer
-
Ad N almost 11 yearsThe parentheses are also changing the meaning of a program in the case of the most vexing parse disambiguation :
B foo(A())
foo is a function (taking a pointer to function as only parameter and returning a B) whereas inB foo((A()))
foo is a B object constructed invoking a constructor taking a A object (which instance is an anonymous temporary in this case). -
Xeo almost 11 years@AdN: That's not an expression anymore, but a declaration.
-
Yakk - Adam Nevraumont about 8 years@R.MartinhoFernandes I think Ramon thought you meant the lambda-dagger, which stabs the return type. (It is usually called an "arrow", but I could see calling
->
a "dagger")