Lambda Expression in C++

A lambda expression, also referred to as lambda, is a simplified notation for defining and using anonymous function object. This is particularly useful when we want to pass a operation as argument to an algorithm.

A lambda expression consists of a sequence of parts;

  • capture list []
  • optional parameter list ()
  • optional mutable specifier
  • optional noexcept specifier
  • optional return type
  • and a body, specifying the code to be executed {}

Usually, we have to base our choice between “algorithm plus lambda” and “for-statement with body”. And of course, for-statement is more easily understood for beginners.

Advertisements