Home > Backend Development > C++ > body text

Is Capturing a Reference by Reference in C 11 Lambdas Guaranteed to Output the Updated Value?

Patricia Arquette
Release: 2024-11-04 20:50:02
Original
328 people have browsed it

Is Capturing a Reference by Reference in C  11 Lambdas Guaranteed to Output the Updated Value?

Capturing References by Reference in C 11 Lambdas

This code snippet demonstrates the capture of an integer reference by reference within a lambda expression:

<code class="cpp">#include <functional>
#include <iostream>

std::function<void()> make_function(int& x) {
    return [&]{ std::cout << x << std::endl; };
}

int main() {
    int i = 3;
    auto f = make_function(i);
    i = 5;
    f();
}</code>
Copy after login

Is this code guaranteed to output 5 without invoking undefined behavior?

The code is indeed guaranteed to work without invoking undefined behavior. Contrary to capturing by value ([= x]), capturing by reference ([&x]) leverages a unique exception within the C standard that allows references to persist outside their lifetime. This is because references captured by lambda expressions are implemented as member access to the lambda closure type.

Scope and Lifetime Considerations

It's important to note that the reaching scope rules for lambdas are purely syntactic and do not play a role in this scenario. The referenced entity, x, is within the reaching scope of the lambda and can be captured.

Standard Wording

According to [expr.prim.lambda]/17 of the C standard, only expressions captured by copy undergo transformation into member access on the lambda closure type. Expressions captured by reference are simply left unmodified and continue to denote the original entity.

Furthermore, the standard does not explicitly address the issue of references being used outside their lifetime. However, since there is no penalty for using a reference outside its lifetime (unless referenced from its own initializer or a preceding class member), this code is allowed to function as intended.

Conclusion

Capturing a reference by reference in a C 11 lambda is guaranteed to work as expected, outputting the updated value of the referenced integer without invoking undefined behavior. This is based on the standard's definition of lambda capture and the established lack of penalties for using references outside their lifetime within certain specific contexts.

The above is the detailed content of Is Capturing a Reference by Reference in C 11 Lambdas Guaranteed to Output the Updated Value?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!