285

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++?

Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • 103
    Hey! That's mine. ["`Don't sweat it. We have int main(){(([](){})());} which is valid C++"`](http://chat.stackoverflow.com/transcript/message/6169551#6169551) (Nov 9th in chat) – sehe Nov 28 '12 at 10:50
  • 33
    it's a c++11 lambda closure –  Nov 28 '12 at 10:51
  • 1
    Read about C++ lambdas, this is what `[](){}` is (an empty lambda function). – Ramon Snir Nov 28 '12 at 10:52
  • 4
    6 years of C++, and I've never learned lambdas... I guess that's next on my to-do list. – Mysticial Nov 28 '12 at 10:52
  • 2
    Also: ["`@Mysticial int main()??<((??(??)()???>)());??>`"](http://chat.stackoverflow.com/transcript/message/6169577#6169577) - much more convincing – sehe Nov 28 '12 at 10:53
  • 8
    @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). – SChepurin Nov 28 '12 at 11:25
  • @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. – Mysticial Nov 28 '12 at 11:26
  • 14
    @Mysticial - "6 years of C++" -- lambdas were just added in C++11, so nobody has experience with them before a year or so ago. – Pete Becker Nov 28 '12 at 14:45
  • 58
    The URL here is quite amusing: "how-is-int-main-valid-c" – tckmn Apr 06 '14 at 22:29
  • Is this equation correct {(([ = ](){})());}? – Trismegistos Jul 02 '14 at 11:44
  • just for info, this will compile on gcc (using `g++` command). with `g++ -std=c++11 file.c` – Avinash R Aug 01 '14 at 18:00
  • 2
    int main(){ return !<:]()<%;[=:>(){<:&](){;%>();%>();}; } is still valid. I don't even remember where I have found it(somewhere on so). – guest-418 Jan 05 '15 at 16:02
  • 3
    I used to think JavaScript is the only esoteric programming language widely used in production... – Fermat's Little Student Dec 08 '15 at 23:45
  • It is also valid: `int ((main)()){(([](){})());}` but I don't brag about it. – AnArrayOfFunctions Nov 28 '18 at 01:53
  • N​­​­​­o​­​­​­​­ –  May 14 '20 at 02:26

1 Answers1

295

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)).

Community
  • 1
  • 1
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • 33
    @R.MartinhoFernandes: It was still stuck in someone, so I had to go and retrieve it. – Xeo Nov 28 '12 at 10:54
  • 1
    I 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 – sehe Nov 28 '12 at 10:56
  • 2
    The 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 in `B foo((A()))` foo is a B object constructed invoking a constructor taking a A object (which instance is an anonymous temporary in this case). – Ad N Aug 23 '13 at 11:05
  • 1
    @AdN: That's not an expression anymore, but a declaration. – Xeo Aug 23 '13 at 12:02
  • @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") – Yakk - Adam Nevraumont Mar 28 '16 at 14:36