Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Lambda expressions(since C++11)

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
      Expressions
      General
      Literals
      Operators
      Conversions
       
       

      Constructs aclosure (an unnamed function object capable of capturing variables in scope).

      Contents

      [edit]Syntax

      [edit]Lambda expressions without an explicit template parameter list (possibly non-generic)
      [captures ]front-attr (optional)(params )specs (optional)except (optional)
      back-attr (optional)trailing (optional)requires (optional)contract-specs (optional){body}
      (1)
      [captures ] {body} (2)(until C++23)
      [captures ]front-attr (optional)trailing (optional)contract-specs (optional){body} (2)(since C++23)
      [captures ]front-attr (optional)except
      back-attr (optional)trailing (optional)contract-specs (optional){body}
      (3)(since C++23)
      [captures ]front-attr (optional)specsexcept (optional)
      back-attr (optional)trailing (optional)contract-specs (optional){body}
      (4)(since C++23)
      [edit]Lambda expressions with an explicit template parameter list (always generic)(since C++20)
      [captures ] <tparams >t-requires (optional)
      front-attr (optional)(params )specs (optional)except (optional)
      back-attr (optional)trailing (optional)requires (optional)contract-specs (optional){body}
      (1)
      [captures ] <tparams >t-requires (optional){body} (2)(until C++23)
      [captures ] <tparams >t-requires (optional)
      front-attr (optional)trailing (optional)contract-specs (optional){body}
      (2)(since C++23)
      [captures ] <tparams >t-requires (optional)front-attr (optional)except
      back-attr (optional)trailing (optional)contract-specs (optional){body}
      (3)(since C++23)
      [captures ] <tparams >t-requires (optional)front-attr (optional)specsexcept (optional)
      back-attr (optional)trailing (optional)contract-specs (optional){body}
      (4)(since C++23)
      1) The lambda expression with a parameter list.
      2-4) The lambda expression without a parameter list.
      2) The simplest syntax.back-attr cannot be applied.
      3,4)back-attr can only be applied if any ofspecs andexcept is present.

      [edit]Explanation

      captures - Specifies the entities to becaptured.
      tparams - A non-empty comma-separated list oftemplate parameters, used to provide names to the template parameters of a generic lambda (seeClosureType::operator() below).
      t-requires - Addsconstraints totparams.

      Ift-requires ends with an attribute specifier sequence, the attributes in the sequence are treated as attributes infront-attr.

      (since C++23)
      front-attr -(since C++23) Anattribute specifier sequence applies tooperator() of the closure type (and thus the[[noreturn]] attribute can be used).
      params - Theparameter list ofoperator() of the closure type.

      It can have anexplicit object parameter.

      (since C++23)
      specs - A list of the following specifiers, each specifier is allowed at most once in each sequence.
      SpecifierEffect
      mutableAllowsbody to modify the objects captured by copy, and to call their non-const member functions.
      • Cannot be used if an explicit object parameter is present.
      (since C++23)
      constexpr     
      (since C++17)
      Explicitly specifies thatoperator() is aconstexpr function.
      • Ifoperator() satisfy all constexpr function requirements,operator() will be constexpr even ifconstexpr is not present.
      consteval
      (since C++20)
      Specifies thatoperator() is animmediate function.
      • consteval andconstexpr cannot be specified at the same time.
      static
      (since C++23)
      Specifies thatoperator() is astatic member function.
      • static andmutable cannot be specified at the same time.
      • Cannot be used ifcaptures is not empty, or an explicit object parameter is present.
      except - Provides thedynamic exception specification or(until C++20) thenoexcept specifier foroperator() of the closure type.
      back-attr - Anattribute specifier sequence applies to the type ofoperator() of the closure type (and thus the[[noreturn]] attribute cannot be used).
      trailing -->ret, whereret specifies the return type.
      requires -(since C++20) Addsconstraints tooperator() of the closure type.
      contract-specs -(since C++26) A list offunction contract specifiers foroperator() of the closure type.
      body - The function body.


      Ifauto is used as a type of a parameter or an explicit template parameter list is provided(since C++20), the lambda is ageneric lambda.

      (since C++14)

      A variable__func__ is implicitly defined at the beginning ofbody, with semantics as describedhere.

      [edit]Closure type

      The lambda expression is a prvalue expression of unique unnamed non-union non-aggregate class type, known asclosure type, which is declared (for the purposes ofADL) in the smallest block scope, class scope, or namespace scope that contains the lambda expression.

      The closure type is astructural type if and only ifcaptures is empty.

      (since C++20)

      The closure type has the following members, they cannot beexplicitly instantiated,explicitly specialized, or(since C++14) named in afriend declaration:

      ClosureType::operator()(params)

      ret operator()(params){ body}
      (static and const may be present, see below)
      template<template-params>
      ret operator()(params){ body}
      (since C++14)
      (generic lambda, static and const may be present, see below)

      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).

      The parameter list ofoperator() isparams if it is provided, otherwise the parameter list is empty.

      The return type ofoperator() is the type specified intrailing .

      Iftrailing is not provided, the return type ofoperator() is automaticallydeduced.[1]

      Unless the keywordmutable was used in the lambda specifiers, or an explicit object parameter is present(since C++23), the cv-qualifier ofoperator() isconst and the objects that were captured by copy are non-modifiable from inside thisoperator(). Explicitconst qualifier is not allowed.operator() is never virtual and cannot have thevolatile qualifier.

      operator() is always constexpr if it satisfies the requirements of aconstexpr function. It is also constexpr if the keywordconstexpr was used in the lambda specifiers.

      (since C++17)

      operator() is animmediate function if the keywordconsteval was used in the lambda specifiers.

      (since C++20)

      operator() is astatic member function if the keywordstatic was used in the lambda specifiers.

      operator() is anexplicit object member function ifparams contains an explicit object parameter.

      (since C++23)


      For each parameter inparams whose type is specified asauto, an invented template parameter is added totemplate-params, in order of appearance. The invented template parameter may be aparameter pack if the corresponding function member ofparams is a function parameter pack.

      // generic lambda, operator() is a template with two parametersauto glambda=[](auto a,auto&& b){return a< b;};bool b= glambda(3,3.14);// OK // generic lambda, operator() is a template with one parameterauto 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.14q();// outputs 1a3.14
      (since C++14)


      If the lambda definition uses an explicit template parameter list, that template parameter list is used withoperator(). For each parameter inparams whose type is specified asauto, an additional invented template parameter is appended to the end of that template parameter list:

      // generic lambda, operator() is a template with two parametersauto glambda=[]<class T>(T a,auto&& b){return a< b;}; // generic lambda, operator() is a template with one parameter packauto f=[]<typename...Ts>(Ts&&...ts){return foo(std::forward<Ts>(ts)...);};
      (since C++20)

      The exception specificationexcept on the lambda expression applies tooperator().

      For the purpose ofname lookup, determining the type and value of thethis pointer and for accessing non-static class members, the body of the closure type'soperator() is considered in the context of the lambda expression.

      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, andoperator() 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*this object captured viathis.

      1. Although function return type deduction is introduced in C++14, its rule is available for lambda return type deduction in C++11.

      ClosureType::operatorret(*)(params)()

      capture-less non-generic lambda
      using F= ret(*)(params);
      operator F()constnoexcept;
      (until C++17)
      using F= ret(*)(params);
      constexpr operator F()constnoexcept;
      (since C++17)
      capture-less generic lambda
      template<template-params>using fptr_t=/* see below */;

      template<template-params>

      operator fptr_t<template-params>()constnoexcept;
      (since C++14)
      (until C++17)
      template<template-params>using fptr_t=/* see below */;

      template<template-params>

      constexpr operator fptr_t<template-params>()constnoexcept;
      (since C++17)

      Thisuser-defined conversion function is only defined if the lambda expression has nocaptures  and has no explicit object parameter(since C++23). It is a public,constexpr,(since C++17) non-virtual, non-explicit, const noexcept member function of the closure object.

      This function is animmediate function if the function call operator (or specialization, for generic lambdas) is an immediate function.

      (since C++20)

      A generic capture-less lambda has a user-defined conversion function template with the same invented template parameter list asoperator().

      void f1(int(*)(int)){}void f2(char(*)(int)){}void h(int(*)(int)){}// #1void h(char(*)(int)){}// #2 auto glambda=[](auto a){return a;};f1(glambda);// OKf2(glambda);// error: not convertibleh(glambda);// OK: calls #1 since #2 is not convertible int&(*fpi)(int*)=[](auto* a)->auto&{return*a;};// OK
      (since C++14)


      The value returned by the conversion function is a pointer to a function with C++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.

      (until C++14)

      The value returned by the conversion function (template) is a pointer to a function with C++language linkage that, when invoked, has the same effect as:

      • for non-generic lambdas, invoking the closure type'soperator() on a default-constructed instance of the closure type.
      • for generic lambdas, invoking the generic lambda's correspondingoperator() specialization on a default-constructed instance of the closure type.
      (since C++14)
      (until C++23)

      The value returned by the conversion function (template) is

      • ifoperator() is static, a pointer to thatoperator() with C++language linkage,
      • otherwise, a pointer to a function with C++language linkage that, when invoked, has the same effect as:
        • for non-generic lambdas, invoking the closure type'soperator() on a default-constructed instance of the closure type.
        • for generic lambdas, invoking the generic lambda's correspondingoperator() specialization on a default-constructed instance of the closure type.
      (since C++23)


      This function is constexpr if the function call operator (or specialization, for generic lambdas) is constexpr.

      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){staticint s;return a;};static_assert(Fwd(NC,3)==3);// error: no specialization can be// constexpr because of static s

      If the closure object'soperator() has a non-throwing exception specification, then the pointer returned by this function has the type pointer to noexcept function.

      (since C++17)

      ClosureType::ClosureType()

      ClosureType()=default;
      (since C++20)
      (only if no captures are specified)
      ClosureType(const ClosureType&)=default;
      ClosureType(ClosureType&&)=default;

      Closure types are notDefaultConstructible. Closure types have no default constructor.

      (until C++20)

      If nocaptures are specified, the closure type has a defaulted default constructor. Otherwise, it has no default constructor (this includes the case when there is acapture-default , even if it does not actually capture anything).

      (since C++20)

      The copy constructor and the move constructor are declared as defaulted and may be implicitly-defined according to the usual rules forcopy constructors andmove constructors.

      ClosureType::operator=(const ClosureType&)

      ClosureType& operator=(const ClosureType&)= delete;
      (until C++20)
      ClosureType& operator=(const ClosureType&)=default;
      ClosureType& operator=(ClosureType&&)=default;
      (since C++20)
      (only if no captures are specified)
      ClosureType& operator=(const ClosureType&)= delete;
      (since C++20)
      (otherwise)

      The copy assignment operator is defined as deleted (and the move assignment operator is not declared). Closure types are notCopyAssignable.

      (until C++20)

      If nocaptures 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 acapture-default , even if it does not actually capture anything).

      (since C++20)

      ClosureType::~ClosureType()

      ~ClosureType()=default;

      The destructor is implicitly-declared.

      ClosureType::Captures

      T1 a;

      T2 b;

      ...

      If the lambda expression captures anything by copy (either implicitly with capture clause[=] or explicitly with a capture that does not include the character &, e.g.[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 aredirect-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 thecapture-default[&] or when using the character &, e.g.[&a, &b, &c]), it is unspecified if additional data members are declared in the closure type, but any such additional members must satisfyLiteralType(since C++17).


      Lambda expressions are not allowed inunevaluated expressions,template arguments,alias declarations,typedef declarations, and anywhere in a function (or function template) declaration except the function body and the function'sdefault arguments.

      (until C++20)

      [edit]Lambda capture

      Thecaptures defines the outside variables that are accessible from within the lambda function body. Its syntax is defined as follows:

      capture-default (1)
      capture-list (2)
      capture-default,capture-list (3)
      capture-default - one of& and=
      capture-list - a comma-separated list ofcapture s


      The syntax ofcapture is defined as follows:

      identifier (1)
      identifier... (2)
      identifierinitializer (3)(since C++14)
      &identifier (4)
      &identifier... (5)
      &identifierinitializer (6)(since C++14)
      this (7)
      *this (8)(since C++17)
      ...identifierinitializer (9)(since C++20)
      &...identifierinitializer (10)(since C++20)
      1) simple by-copy capture
      2) simple by-copy capture that is apack expansion
      3) by-copy capture with aninitializer
      4) simple by-reference capture
      5) simple by-reference capture that is apack 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 thecapture-default is&, subsequent simple captures must not begin with&.

      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 thecapture-default is=, subsequent simple captures must begin with& or be*this(since C++17)orthis(since C++20).

      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:

      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}

      A lambda expression can use a variable without capturing it if the variable

      • is a non-local variable or has static or thread localstorage duration (in which case the variable cannot be captured), or
      • is a reference that has been initialized with aconstant 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 aconstant expression, or
      • is constexpr and has no mutable members.

      The current object (*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=.The implicit capture of*this when the capture default is= is deprecated.(since C++20)

      Only lambda expressions satisfying any of the following conditions may have acapture-default orcapture without initializers:

      (since C++26)

      For such lambda expression, thereaching 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.

      Theidentifier in any capture without an initializer (other than thethis-capture) is looked up using usualunqualified name lookup in thereaching scope of the lambda. The result of the lookup must be avariable with automatic storage duration declared in the reaching scope, or astructured binding whose corresponding variable satisfies such requirements(since C++20). The entity isexplicitly captured.

      A capture with an initializer, calledinit-capture, acts as if it declares and explicitly captures a variable declared with type specifierauto 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 viaauto 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 asx= std::move(x).

      This also makes it possible to capture by const reference, with&cr=std::as_const(x) or similar.

      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.
      (since C++14)

      Ifcaptures has acapture-default and does not explicitly capture the enclosing object (asthis or*this), or an automatic variable that isodr-usable in the lambda body, or astructured binding whose corresponding variable has atomic storage duration(since C++20), it captures the entityimplicitly if the entity is named in apotentially-evaluated expression within an expression (including when the implicitthis-> is added before a use of non-static class member).

      For the purpose of determining implicit captures,typeid is never considered to make its operands unevaluated.

      Entities might be implicitly captured even if they are only named within adiscarded statement after instantiation of the lambda body.

      (since C++17)
      void f(int,constint(&)[2]={}){}// #1void f(constint&,constint(&)[1]){}// #2 struct NoncopyableLiteralType{constexprexplicit NoncopyableLiteralType(int n): n_(n){}    NoncopyableLiteralType(const NoncopyableLiteralType&)= delete; int n_;}; void test(){constint x=17; auto l0=[]{ f(x);};// OK: calls #1, does not capture xauto g0=[](auto a){ f(x);};// same as above auto l1=[=]{ f(x);};// OK: captures x (since P0588R1) and calls #1// the capture can be optimized awayauto g1=[=](auto a){ f(x);};// same as above auto ltid=[=]{typeid(x);};// OK: captures x (since P0588R1)// even though x is unevaluated// 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}; constexpr NoncopyableLiteralType w{42};auto l4=[]{return w.n_;};// OK: w is not odr-used, capture is unnecessary// auto l5 = [=]{ return w.n_; };  // error: w needs to be captured by copy}

      If the body of a lambdaodr-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:

      void f(constint*);void g(){constint 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:

      Run this 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=, the type of any capturable entity is as if it were captured (and thus const-qualification is often added if the lambda is notmutable), even though the entity is in an unevaluated operand and not captured (e.g. indecltype):

      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 thereaching scope of the lambda.

      If a lambda captures the enclosing object (asthis or*this), either the nearest enclosing function must be a non-static member function or the lambda must be in adefault member initializer:

      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(or a specialization of a generic lambda's function call operator)(since C++14) ODR-uses*this or any variable with automatic storage duration, it must be captured by the lambda expression.

      void f1(int i){intconst N=20;auto m1=[=]{intconst 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, onlyvariables are permitted in thecapture-list ):

      class S{int x=0; void f(){int i=0;//  auto l1 = [i, x] { use(i, x); };      // error: x is not a variableauto 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 variablem is treated as an expression(*this).m, and*this is always implicitly captured by reference:

      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}};

      If a lambda expression appears in adefault argument, it cannot explicitly or implicitly capture anything, unless all captures have initializers which satisfy the constraints of an expression appearing in a default argument(since C++14):

      void f2(){int i=1; void g1(int=[i]{return i;}());// error: captures somethingvoid g2(int=[i]{return0;}());// error: captures somethingvoid g3(int=[=]{return i;}());// error: captures something void g4(int=[=]{return0;}());// OK: capture-lessvoid g5(int=[]{return sizeof i;}());// OK: capture-less // C++14void g6(int=[x=1]{return x;}());// OK: 1 can appear//     in a default argumentvoid g7(int=[x= i]{return x;}());// error: i cannot appear//        in a default argument}

      Members ofanonymous unions members cannot be captured.Bit-fields can only be captured by copy.

      If a nested lambdam2 captures something that is also captured by the immediately enclosing lambdam1, thenm2's capture is transformed as follows:

      • if the enclosing lambdam1 captures by copy,m2 is capturing the non-static member ofm1's closure type, not the original variable or*this; ifm1 is not mutable, the non-static data member is considered to be const-qualified.
      • if the enclosing lambdam1 captures by reference,m2 is capturing the original variable or*this.
      Run this 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 123std::cout<< a<< b<< c<<'\n';// prints 234}

      If a lambda captures anything, the type of the explicit object parameter (if any) of the function call operator can only be

      • the closure type,
      • a class type publicly and unambiguously derived from the closure type, or
      • a reference to a possibly cv-qualified such type.
      struct C{template<typename T>    C(T);}; void func(int i){int x=[=](thisauto&&){return i;}();// OKint y=[=](this C){return i;}();// errorint z=[](this C){return42;}();// OK auto lambda=[n=42](thisauto self){return n;};using Closure= decltype(lambda);struct D:private Closure{        D(Closure l): Closure(l){}using Closure::operator();friend Closure;};    D{lambda}();// error}
      (since C++23)

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lambdas200907L(C++11)Lambda expressions
      __cpp_generic_lambdas201304L(C++14)Generic lambda expressions
      201707L(C++20)Explicit template parameter list for generic lambdas
      __cpp_init_captures201304L(C++14)Lambda init-capture
      201803L(C++20)Allow pack expansion in lambda init-capture
      __cpp_capture_star_this201603L(C++17)Lambda capture of*this by value as[=,*this]
      __cpp_constexpr201603L(C++17)constexpr lambda
      __cpp_static_call_operator202207L(C++23)staticoperator() for capture-less lambdas

      The rule for implicit lambda capture is slightly changed by defect reportP0588R1. As of 2023-10, some major implementations have not completely implemented the DR, and thus the old rule, which detectsodr-using, is still used in some cases.

      Old rule before P0588R1

      Ifcaptures has acapture-default and does not explicitly capture the enclosing object (asthis or*this), or an automatic variable that isodr-usable in the lambda body, or astructured binding whose corresponding variable has atomic storage duration(since C++20), it captures the entityimplicitly if the entity is

      • named in apotentially-evaluated expression within an expression that depends on a template parameter of a generic lambda, or
      (since C++14)

      [edit]Example

      This example shows how to pass a lambda to a generic algorithm and how objects resulting from a lambda expression can be stored instd::function objects.

      Run this 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 argumentsauto 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'; constexprint 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_parameterstd::cout<<"C++23 approach to lambda recursion:\n";auto nth_fibonacci3=[](thisauto 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}

      Possible output:

      c: 5 6 7func1: 10func2: 10Emulate `recursive lambda` calls:Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13Alternative approach to lambda recursion:Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 974C++11default argument was not allowed in the
      parameter list of a lambda expression
      allowed
      CWG 1048
      (N3638)
      C++11the return type could only be deduced for lambda
      bodies containing only onereturn statement
      improved the return
      type deduction
      CWG 1249C++11it is not clear that whether the captured member of the
      enclosing non-mutable lambda is consideredconst or not
      consideredconst
      CWG 1557C++11the language linkage of the returned function type of
      the closure type's conversion function was not specified
      it has C++
      language linkage
      CWG 1607C++11lambda expressions could appear in
      function and function template signatures
      not allowed
      CWG 1612C++11members of anonymous unions could be capturednot allowed
      CWG 1722C++11the conversion function for capture-less lambdas
      had unspecified exception specification
      conversion function
      is noexcept
      CWG 1772C++11the semantic of__func__ in lambda body was not clearit refers to the closure
      class's operator()
      CWG 1780C++14it was unclear whether the members of the closure types of generic
      lambdas can be explicitly instantiated or explicitly specialized
      neither is allowed
      CWG 1891C++11closure had a deleted default constructor
      and implicit copy/move constructors
      no default and defaulted
      copy/move constructors
      CWG 1937C++11as for the effect of invoking the result of the
      conversion function, it was unspecified on which
      object calling itsoperator() has the same effect
      on a default-constructed
      instance of the closure type
      CWG 1973C++11the parameter list of the closure type'soperator()
      could refer to the parameter list given intrailing
      can only refer
      toparams
      CWG 2011C++11for a reference captured by reference, it was unspecified
      which entity the identifier of the capture refers to
      it refers to the originally
      referenced entity
      CWG 2095C++11the behavior of capturing rvalue references
      to functions by copy was not clear
      made clear
      CWG 2211C++11the behavior was unspecified if a capture
      has the same name as a parameter
      the program is ill-
      formed in this case
      CWG 2358C++14lambda expressions appearing in default arguments had
      to be capture-less even if all captures are initialized with
      expressions which can appear in default arguments
      allow such lambda
      expressions with captures
      CWG 2509C++17each specifier could have multiple
      occurrences in the specifier sequence
      each specifier can only
      appear at most once in
      the specifier sequence
      CWG 2561C++23a lambdas with explicit object parameter could have a
      conversion function to an undesired function pointer type
      it does not have such
      a conversion funtion
      CWG 2881C++23operator() with explicit parameter could be instantiated for
      a derived class when the inheritance was not public or ambiguous
      made ill-formed
      P0588R1C++11the rule for implicit lambda capture detected odr-usethe detection is simplified

      [edit]See also

      auto specifier(C++11) specifies a type deduced from an expression[edit]
      (C++11)
      copyable wrapper of any copy constructible callable object
      (class template)[edit]
      move-only wrapper of any callable object that supports qualifiers in a given call signature
      (class template)[edit]

      [edit]External links

      Nested function - a function which is defined within another (enclosing) function.
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/lambda&oldid=181273"

      [8]ページ先頭

      ©2009-2025 Movatter.jp