4 ms·
Part 1 says that it is not correct to call a lambda which value-captures a member of a temporary instance of a class. Is that really correct? Isn’t the whole po
by 0db532a0 8y ago
Part 1 says that it is not correct to call a lambda which value-captures a member of a temporary instance of a class. Is that really correct? Isn’t the whole point of value-capturing that the value is copied and preserved regardless of the life of the source of the captured value?
- Negitivefrags 8y agoThis is only related to the use of the this pointer. If you have a member of a class called s then any mention of s inside a member function of the class is really a mention of this->s. After you understand that, you can just apply all the regular rules of lambdas to understand why it gets you in to trouble. this->s is a capture of this, not s. this is a pointer and therefore capturing it by value isn't going to extend the lifetime of the object it points to.
- 0db532a0 8y agoI see from the below link that the ‘STAR this’ pointer would be captured implicitly by reference in this case. This happens regardless of a default capture type if it does exist. To truly capture a member by value, you can (1) make a copy of that value in the enclosing function, thus making it an automatic variable which can be value-captured, (2) value-capture through an initialiser in the capture list or (3) you can indicate ‘STAR this’ (since C++17) as a capture, which then captures the ‘this’ as a value copy and thus transitively your member. https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture https://en.cppreference.com/w/cpp/language/lambda#Lambda_cap...