Is there another advantage of lambda besides convenience?

The title may not be accurate, but I couldn't think of a single short one. (Feel free to suggest or edit my question.)

I was wondering if there is another advantage to using lambda functions other than that it doesn't need to explicitly define (and write) the entire functor class definition, or define a separate function that will (possibly) only be used once. In other words, are lambdas just for convenience, or are there more?

Edit: It's one thing to add to my question. Lambdas allows the programmer to write less, do it more conveniently and are therefore less error prone . Which in itself is a different thing / reason than just convenience, but related to it.

+3


source to share


5 answers


See the complete motivation for lamdas in Suggestion to Add Lambda Functions to the C ++ Standard :



The C ++ standard library algorithms would be nicer to use if C ++ supported lambdas. Lambda functions will allow people to use C ++ Standard Library algorithms in many cases where it is easier to write a for loop nowadays. Many developers don't use functional objects simply because of syntactic overhead.

+8


source


Lambdas are mostly syntactic sugar, but not entirely. One point around lambdas is that they grab arrays by direct subscript initialization [expr.prim.lambda] :

22 - [...] (for array elements, array elements are initialized in direct subscript order.) [...]



It is surprisingly difficult to achieve otherwise; it is necessary to create a package of index parameters using something like std::index_sequence

and the semantics are not exactly the same.

Another thing a lambda can do is capture a (variadic) parameter package; this cannot be done in the general case (since structure members cannot be an extension of a parameter package), except through std::tuple

.

+5


source


Basically: they exist for your convenience.

In addition to Maxim's answer, according to the MSDN article on lambda expressions in C ++ :

When you write code, you probably use function pointers and function objects to solve problems and perform calculations, especially when you are using STL algorithms. Function pointers and function objects advantages and disadvantages - for example, function pointers have minimal syntactic overhead but do not retain state within scope, and functional objects can maintain state but require the syntactic overhead of the class definition.

Lambda combines the advantages of function pointers and function objects and avoids their disadvantages. As functional objects, lambda is flexible and can maintain state, but unlike a function object, its compact syntax does not require a class definition. Using lambdas, you can write code that is less cumbersome and less error prone than code for the equivalent of a functional object.

+2


source


The only advantage of non-core lambdas over the corresponding function object is that they are converted to function pointers.

0


source


  • Using STL algorithms becomes more convenient.

  • If you want to write reusable code in your function, but it might not make sense to nest it in a free function, then lambdas come to the rescue.

For example,

void foo()
{
    auto validate = [] (const std::string& str) -> bool { // do some validation. };
    /// ... code.
    if (!validate("some info"))
    {
       // throw error;
    }

    ////  

     validate("some other info");
}

      

0


source







All Articles