int main(){([](){}()));} Why does it run normally?

Asked 1 years ago, Updated 1 years ago, 79 views

I'm asking you a question because I saw such a strange code.

I don't know why ([](){})()); is compiled normally in the source code below I'm not a function pointer, I'm not an operator.

But if you run it on Visual Studio 2010, nothing comes out...

What is that doing?

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

visual-studio c++ visual-studio-2010 c++11 syntax

2022-09-22 22:29

1 Answers

That code is called empty lambda. I don't really do anything.

First, [](){} is empty lambda expression. For convenience, let 1 = [](){} and (([](){})(); = ((A)();.

Second, parentheses expression in C/C++ have no effect on the code. It does the same thing, whether it's in parentheses or not. So you know up to here (A) = A. Let's say B = (A) and ((A)(); = (B());.

Third, () is (empty) lambda. Then let's say C = B() and (B(); = (C);.

(C) is parentheses the entire expression as we did in the second. Then D = (C) and (C); = D;.

Lastly. ; is the role to end a sentence.


2022-09-22 22:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.