C ++ weird expressions compiled just fine

Why are the following valid C ++ expressions? This is not lambdas

[]{}(); {}[]{}; 

Can someone explain them to me?

+48
c ++
Jun 20 '16 at 12:27
source share
2 answers

The first is a lambda without a list of parameters and a subsequent call. []{} equivalent to [](){} , so the whole line is equivalent

 [](){}(); 



The second is a pair of curly braces that enter and then immediately close the area, followed by the unused lambda definition without a list of parameters:

 { // empty scope } []{}; // lambda 



You can refer to http://en.cppreference.com/w/cpp/language/lambda for lambda definition syntax options.

+77
Jun 20 '16 at 12:29
source share
  • This lambda call

     []{}(); 

    he is equivalent

     [](){}(); 
  • The second is an empty area followed by a (unused) lambda.

Parens are optional for lambdas without parameters.

+40
Jun 20 '16 at 12:29
source share



All Articles