Search⌘ K
AI Features

Lambdas in an Unevaluated Context and Stateless Lambdas

Explore the concepts of unevaluated contexts and stateless lambdas in C++20. Understand how stateless lambdas work, their default construction, and how lambdas can be used with standard associative containers. Learn about template parameters in lambdas and implicit this pointer detection to improve your coding in C++20.

Admittedly, the title of this lesson contains two terms that may be new to you: unevaluated context and stateless lambda. Let me start with unevaluated context.

Unevaluated context

The following code snippet has a function declaration and a function definition.

int add1(int, int); // declaration
int add2(int a, int b) { return a + b; } // definition

Function add1 is declared, while add2 is defined. This means, if you use add1 in an evaluated context, for example, by invoking it, you get a link-time error. The key observation is that you can use add1 in unevaluated contexts, such as ...