Similarities between a Lambda and a class

To understand what the lambda function consists of, one can view it as a regular class with restrictions:

  • The class only consists of one member function.
  • The capture block is a combination of the class's member variables and its constructor.
  • Each lambda function has its own unique type. Even if two lambda functions are plain clones of each other, they still have their own unique type.

The following table shows a simple lambda function, is_above, and a class which corresponds to the lambda function. The left column uses capture by value and the right column shows capture by reference.

Lambda with capture by value...

Lambda with capture by reference...

auto th = 3;auto is_above = [th

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.