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

15,548

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

Share:
15,548

Related videos on Youtube

Mysticial
Author by

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, 2022

Comments

  • Mysticial
    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
      sehe over 11 years
    • Admin
      Admin over 11 years
      it's a c++11 lambda closure
    • Ramon Snir
      Ramon Snir over 11 years
      Read about C++ lambdas, this is what [](){} is (an empty lambda function).
    • Mysticial
      Mysticial over 11 years
      6 years of C++, and I've never learned lambdas... I guess that's next on my to-do list.
    • sehe
      sehe over 11 years
    • SChepurin
      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
      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
      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
      tckmn about 10 years
      The URL here is quite amusing: "how-is-int-main-valid-c"
    • Trismegistos
      Trismegistos almost 10 years
      Is this equation correct {(([ = ](){})());}?
    • Avinash R
      Avinash R almost 10 years
      just for info, this will compile on gcc (using g++ command). with g++ -std=c++11 file.c
    • guest-418
      guest-418 over 9 years
      int main(){ return !<:]()<%;[=:>(){<:&](){;%>();%>();}; } is still valid. I don't even remember where I have found it(somewhere on so).
    • Fermat's Little Student
      Fermat's Little Student over 8 years
      I used to think JavaScript is the only esoteric programming language widely used in production...
    • AnArrayOfFunctions
      AnArrayOfFunctions over 5 years
      It is also valid: int ((main)()){(([](){})());} but I don't brag about it.
    • Admin
      Admin about 4 years
      N​­​­​­o​­​­​­​­
  • Xeo
    Xeo over 11 years
    @R.MartinhoFernandes: It was still stuck in someone, so I had to go and retrieve it.
  • sehe
    sehe over 11 years
    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
  • Ad N
    Ad N almost 11 years
    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).
  • Xeo
    Xeo almost 11 years
    @AdN: That's not an expression anymore, but a declaration.
  • Yakk - Adam Nevraumont
    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")