With C ++ lambdas, what are the rules for capturing links by reference?

With C ++ lambdas, what happens when you commit a link by reference? Are you fixing a link to a local object on the stack (the link itself) or a link to the object being referenced? For example, in the following code:

int& TestClass::returnReference()
{
        static int i=0;
        return i;
}

std::function<void ()> TestClass::testFunction()
{
        int& memberRef = this->someIntMember;
        int& intRef = returnReference();

        auto lambda =
        [&]
        {
                // What happens when you capture a reference by reference
                // like memberRef or intRef?
        };

        return lambda;
}
+4
source share
1 answer

The standard actually instructed him to capture the variable, not what she was talking about. It was a mistake in the standard, and the only case in C ++ where this could happen.

There is a bug report and a proposed resolution (thanks @tc) that changed it to capture the indicated target.

, , (, , this), , . , , , , , locals/globals, .

, , , , . , .

+6

All Articles