[expr.prim.lambda.general] (original) (raw)

7 Expressions [expr]

7.5 Primary expressions [expr.prim]

7.5.6 Lambda expressions [expr.prim.lambda]

7.5.6.1 General [expr.prim.lambda.general]

lambda-specifier:
consteval
constexpr
mutable
static

A lambda-expression provides a concise way to create a simple function object.

[Example 1: #include <algorithm> #include <cmath> void abssort(float* x, unsigned N) { std::sort(x, x + N, [](float a, float b) { return std::abs(a) < std::abs(b); });} — _end example_]

A lambda-expression is a prvalue whose result object is called the closure object.

[Note 2:

Such ambiguous cases cannot have valid semantics because the constraint expression would not have type bool.

[Example 2: auto x = []<class T> requires T::operator int [[some_attribute]] (int) { } — _end example_]

— _end note_]

A lambda-specifier-seqshall contain at most one of each lambda-specifier and shall not contain both constexpr and consteval.

If the lambda-declarator contains an explicit object parameter ([dcl.fct]), then no lambda-specifier in the lambda-specifier-seqshall be mutable or static.

The lambda-specifier-seq shall not contain both mutable and static.

[Note 4:

In that case, the return type is deduced from return statements as described in [dcl.spec.auto].

— _end note_]

[Example 3: auto x1 = [](int i) { return i; }; auto x2 = []{ return { 1, 2 }; }; int j;auto x3 = [&]()->auto&& { return j; }; — _end example_]

[Example 4: auto x = [](int i, auto a) { return i; }; auto y = [](this auto self, int i) { return i; }; auto z = []<class T>(int i) { return i; }; — _end example_]