Namespaces
Variants
Views
Actions

cpp/language/lambda

From cppreference.com
< cpp‎ | language
Revision as of 04:46, 9 October 2023 by Nate. (Talk | contribs)

{{title|Lambda expressions {{mark since c++11}}}} {{cpp/language/expressions/functions/navbar}} Constructs a {{enwiki|Closure (computer science)|closure}}: an unnamed function object capable of capturing variables in scope. ===Syntax=== {{sdsc begin}} {{sdsc|num=1|notes={{mark until c++23}}| {{ttb|[}} {{spar|captures}} {{ttb|]}} {{ttb|(}} {{spar|params}} {{ttb|)}}{{mark optional}} {{spar|specs}} {{ttb|{}} {{spar|body}} {{ttb|}}} }} {{sdsc|num=1|notes={{mark since c++23}}| {{ttb|[}} {{spar|captures}} {{ttb|]}} {{spar|attr}} {{ttb|(}} {{spar|params}} {{ttb|)}}{{mark optional}} {{spar|specs}} {{ttb|{}} {{spar|body}} {{ttb|}}} }} {{sdsc|num=2|notes={{mark since c++20}}<br>{{mark until c++23}}| {{ttb|[}} {{spar|captures}} {{ttb|] <}} {{spar|tparams}} {{ttb|>}} {{spar optional|requires}} {{ttb|(}} {{spar|params}} {{ttb|)}}{{mark optional}} {{spar|specs}} {{ttb|{}} {{spar|body}} {{ttb|}<!---->}} }} {{sdsc|num=2|notes={{mark since c++23}}| {{ttb|[}} {{spar|captures}} {{ttb|] <}} {{spar|tparams}} {{ttb|>}} {{spar optional|requires}} {{spar optional|attr}} {{ttb|(}} {{spar|params}} {{ttb|)}}{{mark optional}} {{spar|specs}} {{ttb|{}} {{spar|body}} {{ttb|}<!---->}} }} {{sdsc end}} @1@ Full form. @2@ Same as {{v|1}}, but specifies a generic lambda and explicitly provides a list of template parameters. ===Explanation=== {{par begin}} {{par|{{spar|captures}}|a comma-separated list of zero or more [[#Lambda capture|captures]], optionally beginning with a {{spar|capture-default}}. See [[#Lambda capture|below]] for the detailed description of captures. A lambda expression can use a variable without capturing it if the variable * is a non-local variable or has static or thread local {{rlp|storage duration|storage duration}} (in which case the variable cannot be captured), or * is a reference that has been initialized with a {{rlp|constant expression#Constant expression|constant expression}}. A lambda expression can read the value of a variable without capturing it if the variable * has const non-volatile integral or enumeration type and has been initialized with a {{rlp|constant expression#Constant expression|constant expression}}, or * is {{tt|constexpr}} and has no mutable members.}} {{par|{{spar|tparams}}|a non-empty comma-separated list of {{rlp|template parameters}}, used to provide names to the template parameters of a generic lambda (see {{tt|ClosureType::operator()}} below).}} {{par|{{spar|params}}|The list of parameters, as in {{rlp|function|named functions}}. If omitted, function takes no arguments, as if the parameter list were ().}} {{par|{{spar|specs}}|consists of {{spar|specifiers}}, {{spar|exception}}, {{spar|attr}}, {{spar|trailing-return-type}} and {{spar|requires}} in that order; each of these components is optional.}} {{par|{{spar|specifiers}}|Optional sequence of specifiers. If not provided, the objects captured by copy are const in the lambda body. The following specifiers are allowed at most once in each sequence: * {{ttb|mutable}}: allows {{spar|body}} to modify the objects captured by copy, and to call their non-const member functions {{rrev|since=c++17| * {{ttb|constexpr}}: explicitly specifies that the function call operator or any given operator template specialization is a {{rlp|constexpr#constexpr function|constexpr function}}. When this specifier is not present, the function call operator or any given operator template specialization will be {{tt|constexpr}} anyway, if it happens to satisfy all constexpr function requirements }} {{rrev|since=c++20| * {{ttb|consteval}}: specifies that the function call operator or any given operator template specialization is an {{rlp|consteval|immediate function}}. {{c|consteval}} and {{c|constexpr}} cannot be used at the same time. }} {{rrev|since=c++23| * {{ttb|static}}: specifies that the function call operator or any given operator template specialization is a {{rlp|static#Static member functions|static member function}}. {{c|mutable}} and {{c|static}} cannot be used at the same time, and {{spar|captures}} must be empty if {{c|static}} is present. }}}} {{par|{{spar|exception}}|provides{{rev inl|until=c++20| the {{rlp|except spec|dynamic exception specification}} or}} the {{rlp|noexcept spec|noexcept specifier}} for {{c|operator()}} of the closure type}} {{par|{{spar|attr}}|optional {{rlp|attributes|attribute specifier sequence}}. {{rrev multi|until1=c++23|rev1= An attribute specifier sequence applies to the type of the function call operator or operator template of the closure type. Any attribute so specified does not appertain to the function call operator or operator template itself, but its type. (For example, the {{attr|noreturn}} attribute cannot be used.) |rev2= If an attribute specifier sequence appears before the parameter list, lambda specifiers, or noexcept specifier, it applies to the function call operator or operator template of the closure type (and thus the {{attr|noreturn}} attribute can be used). Otherwise, it applies to the type of the function call operator or operator template.}}}} {{par|{{spar|trailing-return-type}}|{{ttb|->}} {{spar|ret}}, where {{spar|ret}} specifies the return type. If {{spar|trailing-return-type}} is not present, the return type of the closure's {{c|operator()}} is {{rlp|template argument deduction|deduced}} from {{rlp|return}} statements as if for a function whose {{rlp|function#Return type deduction|return type is declared auto}}.}} {{par|{{spar|requires}}|{{mark since c++20}} adds {{rlp|constraints}} to {{c|operator()}} of the closure type}} {{par|{{spar|body}}|Function body}} {{par end}} {{rrev|since=c++14|If {{rlpt|auto}} is used as a type of a parameter{{rev inl|since=c++20| or an explicit template parameter list is provided}}, the lambda is a ''generic lambda''.}} A variable {{c|__func__}} is implicitly defined at the beginning of {{spar|body}}, with semantics as described {{rlp|function#func|here}}. The lambda expression is a prvalue expression of unique unnamed non-union non-aggregate class type, known as ''closure type'', which is declared (for the purposes of {{rlp|adl|ADL}}) in the smallest block scope, class scope, or namespace scope that contains the lambda expression. The closure type has the following members, they cannot be {{rev inl|since=c++14|{{rlp|function template#Explicit instantiation|explicitly instantiated}}, {{rlp|template specialization|explicitly specialized}}, or}} named in a {{rlp|friend|friend declaration}}: {{anchor|closure type fun operator}} {{member|{{small|ClosureType::}}operator()({{spar|params}})|2= {{dcl begin}} {{dcl|notes={{mark|static and const may be present, see below}}|1= ret operator()(params) { body } }} {{dcl|since=c++14|notes={{mark|generic lambda, static and const may be present, see below}}|1= template<template-params> ret operator()(params) { body } }} {{dcl end}} Executes the body of the lambda-expression, when invoked. When accessing a variable, accesses its captured copy (for the entities captured by copy), or the original object (for the entities captured by reference). Unless the keyword {{c|mutable}} was used in the lambda specifiers, the cv-qualifier of the function-call operator or operator template is {{c|const}} and the objects that were captured by copy are non-modifiable from inside this {{c|operator()}}. Explicit {{c|const}} qualifier is not allowed. The function-call operator or operator template is never virtual and cannot have the {{c|volatile}} qualifier. {{rrev|since=c++17| The function-call operator or any given operator template specialization is always {{tt|constexpr}} if it satisfies the requirements of a {{rlp|constexpr|constexpr function}}. It is also constexpr if the keyword {{c|constexpr}} was used in the lambda specifiers. }} {{rrev|since=c++20| The function-call operator or any given operator template specialization is an {{rlp|consteval|immediate function}} if the keyword {{c|consteval}} was used in the lambda specifiers. }} {{rrev|since=c++23| The function-call operator or any given operator template specialization is a {{rlp|static#Static member functions|static member function}} if the keyword {{c|static}} was used in the lambda specifiers. }} {{rrev|since=c++14| For each parameter in {{spar|params}} whose type is specified as {{c|auto}}, an invented template parameter is added to {{spar|template-params}}, in order of appearance. The invented template parameter may be a {{rlp|parameter pack}} if the corresponding function member of {{spar|params}} is a function parameter pack. <source style="width:45em; overflow-x: hidden;" lang=cpp> // generic lambda, operator() is a template with two parameters auto glambda = [](auto a, auto&& b) { return a < b; }; bool b = glambda(3, 3.14); // OK // generic lambda, operator() is a template with one parameter auto vglambda = [](auto printer) { return [=](auto&&... ts) // generic lambda, ts is a parameter pack { printer(std::forward<decltype(ts)>(ts)...); // nullary lambda (takes no parameters): return [=] { printer(ts...); }; }; }; auto p = vglambda([](auto v1, auto v2, auto v3) { std::cout << v1 << v2 << v3; }); auto q = p(1, 'a', 3.14); // outputs 1a3.14 q(); // outputs 1a3.14 </source> }} {{rrev|since=c++20| If the lambda definition uses an explicit template parameter list, that template parameter list is used with {{c|operator()}}. For each parameter in {{spar|params}} whose type is specified as {{tt|auto}}, an additional invented template parameter is appended to the end of that template parameter list: <source style="width:45em; overflow-x: hidden;" lang=cpp> // generic lambda, operator() is a template with two parameters auto glambda = []<class T>(T a, auto&& b) { return a < b; }; // generic lambda, operator() is a template with one parameter pack auto f = []<typename... Ts>(Ts&&... ts) { return foo(std::forward<Ts>(ts)...); }; </source> }} The exception specification {{spar|exception}} on the lambda-expression applies to the function-call operator or operator template. For the purpose of {{rlp|lookup|name lookup}}, determining the type and value of the {{rlp|this|{{tt|this}} pointer}} and for accessing non-static class members, the body of the closure type's function call operator or operator template is considered in the context of the lambda-expression. {{source|1= struct X { int x, y; int operator()(int); void f() { // the context of the following lambda is the member function X::f [=]() -> int { return operator()(this->x + y); // X::operator()(this->x + (*this).y) // this has type X* }; } }; }} ===Dangling references=== If a non-reference entity is captured by reference, implicitly or explicitly, and the function call operator or a specialization of the function call operator template of the closure object is invoked after the entity's lifetime has ended, undefined behavior occurs. The C++ closures do not extend the lifetimes of objects captured by reference. Same applies to the lifetime of the current {{c|*this}} object captured via {{tt|this}}. }} {{member|{{small|ClosureType::}}operator {{spar|ret}}(*)({{spar|params}})()|2= {{dcl begin}} {{dcl h|capture-less non-generic lambda}} {{dcl rev multi |dcl1= using F = ret(*)(params); operator F() const noexcept; |since2=c++17|dcl2= using F = ret(*)(params); constexpr operator F() const noexcept; }} {{dcl h|capture-less generic lambda}} {{dcl rev multi |since1=c++14|dcl1= template<template-params> using fptr_t = /*see below*/; template<template-params> operator fptr_t<template-params>() const noexcept; |since2=c++17|dcl2= template<template-params> using fptr_t = /*see below*/; template<template-params> constexpr operator fptr_t<template-params>() const noexcept; }} {{dcl end}} This {{rlp|cast operator|user-defined conversion function}} is only defined if the capture list of the lambda-expression is empty. It is a public, {{rev inl|since=c++17|constexpr,}} non-virtual, non-explicit, const noexcept member function of the closure object. {{rrev|since=c++20| This function is an {{rlp|consteval|immediate function}} if the function call operator (or specialization, for generic lambdas) is an immediate function. }} {{rrev|since=c++14| A generic captureless lambda has a user-defined conversion function template with the same invented template parameter list as the function-call operator template. If the return type is empty or auto, it is obtained by return type deduction on the function template specialization, which, in turn, is obtained by {{rlp|template argument deduction}} for conversion function templates. <source style="width:45em; overflow-x: hidden;" lang=cpp> void f1(int (*)(int)) {} void f2(char (*)(int)) {} void h(int (*)(int)) {} // #1 void h(char (*)(int)) {} // #2 auto glambda = [](auto a) { return a; }; f1(glambda); // OK f2(glambda); // error: not convertible h(glambda); // OK: calls #1 since #2 is not convertible int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK </source> }} {{rev begin}} {{rev|until=c++14| The value returned by the conversion function is a pointer to a function with C++ {{rlp|language linkage}} that, when invoked, has the same effect as invoking the closure type's function call operator on a default-constructed instance of the closure type. }} {{rev|since=c++14|until=c++23| The value returned by the conversion function (template) is a pointer to a function with C++ {{rlp|language linkage}} that, when invoked, has the same effect as: * for non-generic lambdas, invoking the closure type's function call operator on a default-constructed instance of the closure type. * for generic lambdas, invoking the generic lambda's corresponding function call operator template specialization on a default-constructed instance of the closure type. }} {{rev|since=c++23| The value returned by the conversion function (template) is * if {{c|operator()}} is static, a pointer to that {{c|operator()}} (specialization) with C++ {{rlp|language linkage}}, * otherwise, a pointer to a function with C++ {{rlp|language linkage}} that, when invoked, has the same effect as: ** for non-generic lambdas, invoking the closure type's function call operator on a default-constructed instance of the closure type. ** for generic lambdas, invoking the generic lambda's corresponding function call operator template specialization on a default-constructed instance of the closure type. }} {{rev end}} {{rrev|since=c++17| This function is constexpr if the function call operator (or specialization, for generic lambdas) is constexpr. <source style="width:45em; overflow-x: hidden;" lang=cpp> auto Fwd = [](int(*fp)(int), auto a) { return fp(a); }; auto C = [](auto a) { return a; }; static_assert(Fwd(C, 3) == 3); // OK auto NC = [](auto a) { static int s; return a; }; static_assert(Fwd(NC, 3) == 3); // error: no specialization can be // constexpr because of static s </source> If the closure object's {{c|operator()}} has a non-throwing exception specification, then the pointer returned by this function has the type pointer to noexcept function. }} }} {{member|{{small|ClosureType::}}ClosureType()|2= {{dcl begin}} {{dcl|since=c++20|notes={{mark|only if no captures are specified}}|1= ClosureType() = default; }} {{dcl|1= ClosureType(const ClosureType&) = default; }} {{dcl|1= ClosureType(ClosureType&&) = default; }} {{dcl end}} {{rrev multi| until1=c++20|rev1= Closure types are not {{named req|DefaultConstructible}}. Closure types have no default constructor. |rev2=If no {{spar|captures}} are specified, the closure type has a defaulted default constructor. Otherwise, it has no default constructor (this includes the case when there is a {{spar|capture-default}}, even if it does not actually capture anything). }} The copy constructor and the move constructor are declared as defaulted and may be implicitly-defined according to the usual rules for {{rlp|copy constructor|copy constructors}} and {{rlp|move constructor|move constructors}}. }} {{member|{{small|ClosureType::}}operator{{=}}(const ClosureType&)|2= {{dcl begin}} {{dcl|until=c++20|1= ClosureType& operator=(const ClosureType&) = delete; }} {{dcl|since=c++20|notes={{mark|only if no captures are specified}}|1= ClosureType& operator=(const ClosureType&) = default; ClosureType& operator=(ClosureType&&) = default; }} {{dcl|since=c++20|notes={{mark|otherwise}}|1= ClosureType& operator=(const ClosureType&) = delete; }} {{dcl end}} {{rrev multi| until1=c++20|rev1= The copy assignment operator is defined as deleted (and the move assignment operator is not declared). Closure types are not {{named req|CopyAssignable}}. |rev2=If no {{spar|captures}} are specified, the closure type has a defaulted copy assignment operator and a defaulted move assignment operator. Otherwise, it has a deleted copy assignment operator (this includes the case when there is a {{spar|capture-default}}, even if it does not actually capture anything). }} }} {{member|{{small|ClosureType::}}~ClosureType()|2= {{ddcl|1= ~ClosureType() = default; }} The destructor is implicitly-declared. }} {{member|{{small|ClosureType::}}{{spar|Captures}}|2= {{ddcl|1= T1 a; T2 b; ... }} If the lambda-expression captures anything by copy (either implicitly with capture clause {{ttb|[{{=}}]}} or explicitly with a capture that does not include the character &, e.g. {{ttb|[a, b, c]}}), the closure type includes unnamed non-static data members, declared in unspecified order, that hold copies of all entities that were so captured. Those data members that correspond to captures without initializers are {{rlp|direct initialization|direct-initialized}} when the lambda-expression is evaluated. Those that correspond to captures with initializers are initialized as the initializer requires (could be copy- or direct-initialization). If an array is captured, array elements are direct-initialized in increasing index order. The order in which the data members are initialized is the order in which they are declared (which is unspecified). The type of each data member is the type of the corresponding captured entity, except if the entity has reference type (in that case, references to functions are captured as lvalue references to the referenced functions, and references to objects are captured as copies of the referenced objects). For the entities that are captured by reference (with the capture-default {{ttb|[&]}} or when using the character &, e.g. {{ttb|[&a, &b, &c]}}), it is unspecified if additional data members are declared in the closure type{{rev inl|since=c++17|, but any such additional members must satisfy {{named req|LiteralType}}}}. }} {{rrev|until=c++20|{{anchor|Lambdas in unevaluated contexts}} Lambda-expressions are not allowed in {{rlp|expressions#Unevaluated expressions|unevaluated expressions}}, {{rlp|template parameters|template arguments}}, {{rlp|type alias|alias declarations}}, {{rlp|typedef|typedef declarations}}, and anywhere in a function (or function template) declaration except the function body and the function's {{rlp|default arguments}}. }} ===Lambda capture=== The {{spar|captures}} is a comma-separated list of zero or more ''captures'', optionally beginning with the {{spar|capture-default}}. The capture list defines the outside variables that are accessible from within the lambda function body. The only capture defaults are * {{ttb|&}} (implicitly capture the used automatic variables by reference) and * {{ttb|{{=}}}} (implicitly capture the used automatic variables by copy). The current object ({{c|*this}}) can be implicitly captured if either capture default is present. If implicitly captured, it is always captured by reference, even if the capture default is {{tt|{{=}}}}. {{rev inl|since=c++20|The implicit capture of {{c|*this}} when the capture default is {{tt|1==}} is deprecated.}} The syntax of an individual capture in {{spar|captures}} is {{sdsc begin}} {{sdsc|num=1|{{spar|identifier}}}} {{sdsc|num=2|{{spar|identifier}} {{ttb|...}}}} {{sdsc|num=3|notes={{mark since c++14}}|{{spar|identifier}} {{spar|initializer}}}} {{sdsc|num=4|{{ttb|&}} {{spar|identifier}}}} {{sdsc|num=5|{{ttb|&}} {{spar|identifier}} {{ttb|...}}}} {{sdsc|num=6|notes={{mark since c++14}}|{{ttb|&}} {{spar|identifier}} {{spar|initializer}}}} {{sdsc|num=7|{{ttb|this}}}} {{sdsc|num=8|{{ttb|*}} {{ttb|this}}|notes={{mark since c++17}}}} {{sdsc|num=9|notes={{mark since c++20}}|{{ttb|...}} {{spar|identifier}} {{spar|initializer}}}} {{sdsc|num=10|notes={{mark since c++20}}|{{ttb|&}} {{ttb|...}} <!--CWG2378-->{{spar|identifier}} {{spar|initializer}}}} {{sdsc end}} @1@ simple by-copy capture @2@ simple by-copy capture that is a {{rlp|parameter pack|pack expansion}} @3@ by-copy capture with an {{rlp|initialization|initializer}} @4@ simple by-reference capture @5@ simple by-reference capture that is a {{rlp|parameter pack|pack expansion}} @6@ by-reference capture with an initializer @7@ simple by-reference capture of the current object @8@ simple by-copy capture of the current object @9@ by-copy capture with an initializer that is a pack expansion @10@ by-reference capture with an initializer that is a pack expansion If the capture-default is {{tt|&}}, subsequent simple captures must not begin with {{tt|&}}. {{source|1= struct S2 { void f(int i); }; void S2::f(int i) { [&] {}; // OK: by-reference capture default [&, i] {}; // OK: by-reference capture, except i is captured by copy [&, &i] {}; // Error: by-reference capture when by-reference is the default [&, this] {}; // OK, equivalent to [&] [&, this, i] {}; // OK, equivalent to [&, i] } }} If the capture-default is {{tt|{{=}}}}, subsequent simple captures must begin with {{tt|&}}{{rev inl|since=c++17| or be {{c|*this}}}} {{rev inl|since=c++20|or {{tt|this}}}}. {{source|1= struct S2 { void f(int i); }; void S2::f(int i) { [=] {}; // OK: by-copy capture default [=, &i] {}; // OK: by-copy capture, except i is captured by reference [=, *this] {}; // until C++17: Error: invalid syntax // since C++17: OK: captures the enclosing S2 by copy [=, this] {}; // until C++20: Error: this when = is the default // since C++20: OK, same as [=] } }} Any capture may appear only once, and its name must be different from any parameter name: {{source|1= struct S2 { void f(int i); }; void S2::f(int i) { [i, i] {}; // Error: i repeated [this, *this] {}; // Error: "this" repeated (C++17) [i] (int i) {}; // Error: parameter and capture have the same name } }} Only lambda-expressions defined at block scope or in a {{rlp|data members#Member initialization|default member initializer}} may have a capture-default or captures without initializers. For such lambda-expression, the ''reaching scope'' is defined as the set of enclosing scopes up to and including the innermost enclosing function (and its parameters). This includes nested block scopes and the scopes of enclosing lambdas if this lambda is nested. The {{spar|identifier}} in any capture without an initializer (other than the {{tt|this}}-capture) is looked up using usual {{rlp|lookup|unqualified name lookup}} in the ''reaching scope'' of the lambda. The result of the lookup must be a {{rlp|object|variable}} with automatic storage duration declared in the reaching scope{{rev inl|since=c++20|, or a {{rlp|structured binding}} whose corresponding variable satisfies such requirements}}. The entity is ''explicitly captured''. {{rrev|since=c++14| A capture with an initializer acts as if it declares and explicitly captures a variable declared with type specifier {{rlpt|auto}} and the same initializer, whose declarative region is the body of the lambda expression (that is, it is not in scope within its initializer), except that: * if the capture is by-copy, the introduced non-static data member of the closure object is another way to refer to that variable; ** in other words, the source variable does not actually exist, and the type deduction via {{c|auto}} and the initialization are applied to the non-static data member; * if the capture is by-reference, the reference variable's lifetime ends when the lifetime of the closure object ends. This is used to capture move-only types with a capture such as {{c|1=x = std::move(x)}}. This also makes it possible to capture by const reference, with {{c|1=&cr = std::as_const(x)}} or similar. {{source|1= int x = 4; auto y = [&r = x, x = x + 1]() -> int { r += 2; return x * x; }(); // updates ::x to 6 and initializes y to 25. }} }} If a capture list has a capture-default and does not explicitly capture the enclosing object (as {{c|this}} or {{c|*this}}), or an automatic variable that is {{rlp|definition#ODR-use|odr-usable}} in the lambda body{{rev inl|since=c++20|, or a {{rlp|structured binding}} whose corresponding variable has atomic storage duration}}, it captures it ''implicitly'' if * the body of the lambda {{rlp|definition#ODR-use|odr-uses}} the entity {{rrev|since=c++14| * or the entity is named in a {{rlp|expressions#Potentially-evaluated expressions|potentially-evaluated}} expression within an expression{{rev inl|until=c++17| that depends on a generic lambda parameter}} (including when the implicit {{c|this->}} is added before a use of non-static class member). For this purpose, the operand of {{rlpt|typeid}} is always considered potentially-evaluated. {{rev inl|since=c++17|Entities might be implicitly captured even if they are only named within a {{rlp|if#Constexpr if|discarded statement}}.}} {{source|1= void f(int, const int (&)[2] = {}) {} // #1 void f(const int&, const int (&)[1]) {} // #2 void test() { const int x = 17; auto g0 = [](auto a) { f(x); }; // OK: calls #1, does not capture x auto g1 = [=](auto a) { f(x); }; // does not capture x in C++14, captures x in C++17 // the capture can be optimized away auto g2 = [=](auto a) { int selector[sizeof(a) == 1 ? 1 : 2] = {}; f(x, selector); // OK: is a dependent expression, so captures x }; auto g3 = [=](auto a) { typeid(a + x); // captures x regardless of // whether a + x is an unevaluated operand }; } }} }} If the body of a lambda {{rlp|definition#ODR-use|odr-uses}} an entity captured by copy, the member of the closure type is accessed. If it is not odr-using the entity, the access is to the original object: {{source|1= void f(const int*); void g() { const int N = 10; [=] { int arr[N]; // not an odr-use: refers to g's const int N f(&N); // odr-use: causes N to be captured (by copy) // &N is the address of the closure object's member N, not g's N }(); } }} If a lambda odr-uses a reference that is captured by reference, it is using the object referred-to by the original reference, not the captured reference itself: {{example|code= #include <iostream> auto make_function(int& x) { return [&] { std::cout << x << '\n'; }; } int main() { int i = 3; auto f = make_function(i); // the use of x in f binds directly to i i = 5; f(); // OK: prints 5 } }} Within the body of a lambda with capture default {{tt|1==}}, the type of any capturable entity is as if it were captured (and thus const-qualification is often added if the lambda is not {{tt|mutable}}), even though the entity is in an unevaluated operand and not captured (e.g. in {{rlpt|decltype}}): {{source|1= void f3() { float x, &r = x; [=] { // x and r are not captured (appearance in a decltype operand is not an odr-use) decltype(x) y1; // y1 has type float decltype((x)) y2 = y1; // y2 has type float const& because this lambda // is not mutable and x is an lvalue decltype(r) r1 = y1; // r1 has type float& (transformation not considered) decltype((r)) r2 = y2; // r2 has type float const& }; } }} Any entity captured by a lambda (implicitly or explicitly) is odr-used by the lambda-expression (therefore, implicit capture by a nested lambda triggers implicit capture in the enclosing lambda). All implicitly-captured variables must be declared within the ''reaching scope'' of the lambda. If a lambda captures the enclosing object (as {{c|this}} or {{c|*this}}), either the nearest enclosing function must be a non-static member function or the lambda must be in a {{rlp|data members#Member initialization|default member initializer}}: {{source|1= struct s2 { double ohseven = .007; auto f() // nearest enclosing function for the following two lambdas { return [this] // capture the enclosing s2 by reference { return [*this] // capture the enclosing s2 by copy (C++17) { return ohseven; // OK } }(); } auto g() { return [] // capture nothing { return [*this] {}; // error: *this not captured by outer lambda-expression }(); } }; }} If a lambda expression {{rev inl|since=c++14|(or an instantiation of a generic lambda's function call operator)}} ODR-uses {{c|*this}} or any variable with automatic storage duration, it must be captured by the lambda expression. {{source|1= void f1(int i) { int const N = 20; auto m1 = [=] { int const M = 30; auto m2 = [i] { int x[N][M]; // N and M are not odr-used // (ok that they are not captured) x[0][0] = i; // i is explicitly captured by m2 // and implicitly captured by m1 }; }; struct s1 // local class within f1() { int f; void work(int n) // non-static member function { int m = n * n; int j = 40; auto m3 = [this, m] { auto m4 = [&, j] // error: j is not captured by m3 { int x = n; // error: n is implicitly captured by m4 // but not captured by m3 x += m; // OK: m is implicitly captured by m4 // and explicitly captured by m3 x += i; // error: i is outside of the reaching scope // (which ends at work()) x += f; // OK: this is captured implicitly by m4 // and explicitly captured by m3 }; }; } }; } }} Class members cannot be captured explicitly by a capture without initializer (as mentioned above, only {{rlp|object|variables}} are permitted in the capture list): {{source|1= class S { int x = 0; void f() { int i = 0; // auto l1 = [i, x] { use(i, x); }; // error: x is not a variable auto l2 = [i, x = x] { use(i, x); }; // OK, copy capture i = 1; x = 1; l2(); // calls use(0,0) auto l3 = [i, &x = x] { use(i, x); }; // OK, reference capture i = 2; x = 2; l3(); // calls use(1,2) } }; }} When a lambda captures a member using implicit by-copy capture, it does not make a copy of that member variable: the use of a member variable {{tt|m}} is treated as an expression {{c|(*this).m}}, and {{c|*this}} is always implicitly captured by reference: {{source|1= class S { int x = 0; void f() { int i = 0; auto l1 = [=] { use(i, x); }; // captures a copy of i and // a copy of the this pointer i = 1; x = 1; l1(); // calls use(0, 1), as if // i by copy and x by reference auto l2 = [i, this] { use(i, x); }; // same as above, made explicit i = 2; x = 2; l2(); // calls use(1, 2), as if // i by copy and x by reference auto l3 = [&] { use(i, x); }; // captures i by reference and // a copy of the this pointer i = 3; x = 2; l3(); // calls use(3, 2), as if // i and x are both by reference auto l4 = [i, *this] { use(i, x); }; // makes a copy of *this, // including a copy of x i = 4; x = 4; l4(); // calls use(3, 2), as if // i and x are both by copy } }; }} {{rrev|since=c++11| If a lambda expression appears in a {{rlp|default arguments|default argument}}, it cannot explicitly or implicitly capture anything{{rev inl|since=c++14|, unless all captures have initializers which satisfy the constraints of an expression appearing in a default argument}}: {{source|1= void f2() { int i = 1; void g1(int = ([i] { return i; })()); // error: captures something void g2(int = ([i] { return 0; })()); // error: captures something void g3(int = ([=] { return i; })()); // error: captures something void g4(int = ([=] { return 0; })()); // OK: capture-less void g5(int = ([] { return sizeof i; })()); // OK: capture-less // C++14 void g6(int = ([x = 1] { return x; }))(); // OK: 1 can appear // in a default argument void g7(int = ([x = i] { return x; }))(); // error: i cannot appear // in a default argument } }} }} Members of {{rlp|union|anonymous unions}} members cannot be captured. {{rlp|bit field|Bit-fields}} can only be captured by copy. If a nested lambda {{tt|m2}} captures something that is also captured by the immediately enclosing lambda {{tt|m1}}, then {{tt|m2}}'s capture is transformed as follows: * if the enclosing lambda {{tt|m1}} captures by copy, {{tt|m2}} is capturing the non-static member of {{tt|m1}}'s closure type, not the original variable or {{c|*this}}; if {{tt|m1}} is not mutable, the non-static data member is considered to be const-qualified. * if the enclosing lambda {{tt|m1}} captures by reference, {{tt|m2}} is capturing the original variable or {{c|*this}}. {{example |code= #include <iostream> int main() { int a = 1, b = 1, c = 1; auto m1 = [a, &b, &c]() mutable { auto m2 = [a, b, &c]() mutable { std::cout << a << b << c << '\n'; a = 4; b = 4; c = 4; }; a = 3; b = 3; c = 3; m2(); }; a = 2; b = 2; c = 2; m1(); // calls m2() and prints 123 std::cout << a << b << c << '\n'; // prints 234 } }} ===Notes=== {{ftm begin|core=1|std=1|comment=1|sort=1}} {{ftm|value=200907L|std=C++11|__cpp_lambdas|Lambda expressions}} {{ftm|value=201304L|std=C++14|__cpp_generic_lambdas|Generic lambda expressions|rowspan="2"}} {{ftm|value=201707L|std=C++20|-|Explicit template parameter list for generic lambdas}} {{ftm|value=201304L|std=C++14|__cpp_init_captures|Lambda init-capture|rowspan="2"}} {{ftm|value=201803L|std=C++20|-|Allow pack expansion in lambda init-capture}} {{ftm|value=201603L|std=C++17|__cpp_capture_star_this|Lambda capture of {{c|*this}} by value as {{c|1=[=, *this]}}}} {{ftm|value=201603L|std=C++17|__cpp_constexpr|{{c|constexpr}} lambda}} {{ftm|value=202207L|std=C++23|__cpp_static_call_operator|{{c|static operator()}} for captureless lambdas}} {{ftm end}} ===Example=== {{example |This example shows how to pass a lambda to a generic algorithm and how objects resulting from a lambda expression can be stored in {{lc|std::function}} objects. |code= #include <algorithm> #include <functional> #include <iostream> #include <vector> int main() { std::vector<int> c{1, 2, 3, 4, 5, 6, 7}; int x = 5; c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; }), c.end()); std::cout << "c: "; std::for_each(c.begin(), c.end(), [](int i) { std::cout << i << ' '; }); std::cout << '\n'; // the type of a closure cannot be named, but can be inferred with auto // since C++14, lambda could own default arguments auto func1 = [](int i = 6) { return i + 4; }; std::cout << "func1: " << func1() << '\n'; // like all callable objects, closures can be captured in std::function // (this may incur unnecessary overhead) std::function<int(int)> func2 = [](int i) { return i + 4; }; std::cout << "func2: " << func2(6) << '\n'; constexpr int fib_max {8}; std::cout << "Emulate `recursive lambda` calls:\nFibonacci numbers: "; auto nth_fibonacci = [](int n) { std::function<int(int, int, int)> fib = [&](int n, int a, int b) { return n ? fib(n - 1, a + b, a) : b; }; return fib(n, 0, 1); }; for (int i{1}; i <= fib_max; ++i) std::cout << nth_fibonacci(i) << (i < fib_max ? ", " : "\n"); std::cout << "Alternative approach to lambda recursion:\nFibonacci numbers: "; auto nth_fibonacci2 = [](auto self, int n, int a = 0, int b = 1) -> int { return n ? self(self, n - 1, a + b, a) : b; }; for (int i{1}; i <= fib_max; ++i) std::cout << nth_fibonacci2(nth_fibonacci2, i) << (i < fib_max ? ", " : "\n"); #ifdef __cpp_explicit_this_parameter std::cout << "C++23 approach to lambda recursion:\n"; auto nth_fibonacci3 = [](this auto self, int n, int a = 0, int b = 1) -> int { return n ? self(n - 1, a + b, a) : b; }; for (int i{1}; i <= fib_max; ++i) std::cout << nth_fibonacci3(i) << (i < fib_max ? ", " : "\n"); #endif } |p=true |output= c: 5 6 7 func1: 10 func2: 10 Emulate `recursive lambda` calls: Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13 Alternative approach to lambda recursion: Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13 }} ===Defect reports=== {{dr list begin}} {{dr list item|wg=cwg|dr=974|std=C++11|before=default argument was not allowed in the<br>parameter list of a lambda expression|after=allowed}} {{dr list item|wg=cwg|dr=975<!-- the actual resolution is in N3638|std=C++11|before=the return type of closure's {{c|operator()}} was only<br>deduced if lambda body contains a single return|after=deduced as if for C++14<br>auto-returning function}} {{dr list item|wg=cwg|dr=1249|std=C++11|before=it is not clear that whether the captured member of the<br>enclosing non-mutable lambda is considered {{tt|const}} or not|after=considered {{tt|const}}}} {{dr list item|wg=cwg|dr=1557|std=C++11|before=the language linkage of the returned function type of<br>the closure type's conversion function was not specified|after=it has C++<br>language linkage}} {{dr list item|wg=cwg|dr=1607|std=C++11|before=lambda expressions could appear in<br>function and function template signatures|after=not allowed}} {{dr list item|wg=cwg|dr=1612|std=C++11|before=members of anonymous unions could be captured|after=not allowed}} {{dr list item|wg=cwg|dr=1722|std=C++11|before=the conversion function for captureless lambdas<br>had unspecified exception specification|after=conversion function<br>is noexcept}} {{dr list item|wg=cwg|dr=1772|std=C++11|before=the semantic of {{c|__func__}} in lambda body was not clear|after=it refers to the closure<br>class's operator()}} {{dr list item|wg=cwg|dr=1780|std=C++14|before=it was unclear whether the members of the closure types of generic<br>lambdas can be explicitly instantiated or explicitly specialized|after=neither is allowed}} {{dr list item|wg=cwg|dr=1891|std=C++11|before=closure had a deleted default constructor<br>and implicit copy/move constructors|after=no default and defaulted<br>copy/move constructors}} {{dr list item|wg=cwg|dr=1937|std=C++11|before=as for the effect of invoking the result of the conversion function, it was<br>unspecified on which object calling its operator() has the same effect|after=on a default-constructed<br>instance of the closure type}} {{dr list item|wg=cwg|dr=2011|std=C++11|before=for a reference captured by reference, it was unspecified<br>which entity the identifier of the capture refers to|after=it refers to the originally<br>referenced entity}} {{dr list item|wg=cwg|dr=2095|std=C++11|before=the behavior of capturing rvalue references<br>to functions by copy was not clear|after=made clear}} {{dr list item|wg=cwg|dr=2211|std=C++11|before=the behavior was unspecified if a capture<br>has the same name as a parameter|after=the program is ill-<br>formed in this case}} {{dr list item|wg=cwg|dr=2358|std=C++14|before=lambda expressions appearing in default arguments had<br>to be capture-less even if all captures are initialized with<br>expressions which can appear in default arguments|after=allow such lambda<br>expressions with captures}} {{dr list item|wg=cwg|dr=2509|std=C++17|before=each specifier could have multiple<br>occurrences in the specifier sequence|after=each specifier can only<br>appear at most once in<br>the specifier sequence<!-- Note: the C++11/14 syntaxes do not use decl-specifier-seq becuase the only specifier allowed then was 'mutable'}} {{dr list end}} ===See also=== {{dsc begin}} {{dsc inc|cpp/language/dsc auto}} {{dsc inc|cpp/utility/functional/dsc function}} {{dsc inc|cpp/utility/functional/dsc move_only_function}} {{dsc end}} ===External links=== {{eli|{{enwiki|Nested function}} - a function which is defined within another (''enclosing'') function.}} {{langlinks|de|es|fr|it|ja|pt|ru|zh}}