In a lambda, how reference is being captured by value

前端 未结 1 1242
别跟我提以往
别跟我提以往 2021-02-03 17:07

If the variable of reference-type is being captured in a lambda by value, is it being captured by reference or by value?

Small sample with question:

#inclu         


        
相关标签:
1条回答
  • 2021-02-03 17:53

    By value. Compilable example:

    class C
    {
    public:
        C()
        {
            i = 0;
        }
    
        C(const C & source)
        {
            std::cout << "Copy ctor called\n";
            i = source.i;
        }
    
        int i;
    };
    
    void test(C & c)
    {
        c.i = 20;
    
        auto lambda = [=]() mutable {
    
            c.i = 55;
        };
        lambda();
    
        std::cout << c.i << "\n";
    }
    
    int main(int argc, char * argv[])
    {
        C c;
        test(c);
    
        getchar();
    }
    

    Result:

    Copy ctor called
    20

    I guess, that this paragraph of the C++ standard applies:

    5.1.2 Lambda expressions

    (...) 14. An entity is captured by copy if it is implicitly captured and the capture-default is = or if it is explicitly captured with a capture that does not include an &. For each entity captured by copy, an unnamed nonstatic data member is declared in the closure type. The declaration order of these members is unspecified. The type of such a data member is the type of the corresponding captured entity if the entity is not a reference to an object, or the referenced type otherwise. [ Note: If the captured entity is a reference to a function, the corresponding data member is also a reference to a function. —end note]

    That actually makes sense - if local variables are passed by value and parameter passed by reference "acts" as a local variable in function, why would it be passed by reference instead of value?

    0 讨论(0)
提交回复
热议问题