Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper indentation for C++11 lambdas

Is there an accepted "standard" way to format lambda-expressions in C++ >= 11 ? Especially when put in generic algorithms for instance.

For instance :

1)

auto it = std::find_if(myVec.begin(),
                       myVec.end(),
                       [id = 42] (const Element& e)
{ return e.id() == id;});

Or 2)

auto it = std::find_if(myVec.begin(),
                       myVec.end(),
                       [id = 42] 
                       (const Element& e)
                       { return e.id() == id;});

Or 3)

auto it = std::find_if(myVec.begin(),
                       myVec.end(),
                       [id = 42] (const Element& e)
                       { 
                           return e.id() == id;
                       });

Or 4)

auto it = std::find_if(myVec.begin(),
                       myVec.end(),
                       [id = 42] (const Element& e)
{ 
    return e.id() == id;
});

Or any other combination of carriage returns, spaces, tabs... Note : I use Allman style in my code, so ideally it would be "fitting in the same style".

like image 218
Jean-Michaël Celerier Avatar asked Oct 22 '14 15:10

Jean-Michaël Celerier


People also ask

What is the correct syntax for lambda expression in C++11?

Lambdas can both capture variables and accept input parameters. A parameter list (lambda declarator in the Standard syntax) is optional and in most aspects resembles the parameter list for a function. auto y = [] (int first, int second) { return first + second; };

What are square brackets [] used for in a lambda declaration?

The square brackets specify which variables are "captured" by the lambda, and how (by value or reference). Capture means that you can refer to the variable outside the lambda from inside the lambda.

How do you declare lambda in C++?

Creating a Lambda Expression in C++auto greet = []() { // lambda function body }; Here, [] is called the lambda introducer which denotes the start of the lambda expression. () is called the parameter list which is similar to the () operator of a normal function.

Can lambdas be Inlined?

Inlinable lambdas can only be called inside inline functions or passed as inlinable arguments. noinline lambdas, however, can be manipulated in any way you like, including being stored in fields or passed around.


1 Answers

I have always preferred to endow relative levels of indent with their own semantic value and vertically align a closing delimiter with the line that contains its matching opening delimiter. This makes complex statements (like those with lambda expressions as arguments) easier to read:

auto it = std::find_if(
  myVec.begin(),
  myVec.end(),
  [id = 42] (const Element& e){ return e.id() == id;}
);

or (if, for example, the lambda body was too long for one line)

auto it = std::find_if(
  myVec.begin(),
  myVec.end(),
  [id = 42] (const Element& e){ 
    return e.id() == id;
  }
);
like image 76
Iron Savior Avatar answered Sep 18 '22 10:09

Iron Savior