CWG Issue 2011 (original) (raw)

This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 118e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-11-05


2011. Unclear effect of reference capture of reference

Section: 7.5.6.3 [expr.prim.lambda.capture]Status: C++17Submitter: Ville VoutilainenDate: 2014-09-28

[Adopted at the February/March, 2017 meeting as document P0613R0.]

The Standard refers to capturing “entities,” and a reference is an entity. However, it is not clear what capturing a reference by reference would mean. In particular, 7.5.6 [expr.prim.lambda] paragraph 16 says,

It is unspecified whether additional unnamed non-static data members are declared in the closure type for entities captured by reference.

If a reference captured by reference is not represented by a member, it is hard to see how something like the following example could work:

#include #include

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

Should this be undefined behavior or should it print 5?

Proposed resolution (November, 2014) [SUPERSEDED]:

  1. Change 7.5.6 [expr.prim.lambda] paragraph 18 as follows:

Every id-expression within the compound-statement of a _lambda-expression_that is an odr-use (6.3 [basic.def.odr]) of an entity captured by copy is transformed into an access to the corresponding unnamed data member of the closure type. [_Note:_An id-expression that is not an odr-use refers to the original entity, never to a member of the closure type. Furthermore, such an id-expression does not cause the implicit capture of the entity. —_end note_] If this is captured, each odr-use of this is transformed into an access to the corresponding unnamed data member of the closure type, cast (7.6.3 [expr.cast]) to the type of this. [Note: The cast ensures that the transformed expression is a prvalue. —_end note_] An id-expression within the compound-statement of a _lambda-expression_that is an odr-use of a reference captured by reference refers to the entity to which the captured reference is bound and not to the captured reference. [Note: Such odr-uses are not invalidated by the end of the captured reference's lifetime. —_end note_][Example:

void f(const int*); void g() { const int N = 10; [=] { int arr[N]; // OK: not an odr-use, refers to automatic variable f(&N); // OK: causes N to be captured; &N points to the // corresponding member of the closure type }; } auto h(int &r) { return & { ++r; // Valid after h returns if the lifetime of the // object to which r is bound has not ended }; }

—_end example_]

  1. Change 7.5.6 [expr.prim.lambda] paragraph 23 as follows:

[Note: If an a non-reference entity is implicitly or explicitly captured by reference, invoking the function call operator of the corresponding lambda-expression after the lifetime of the entity has ended is likely to result in undefined behavior. —_end note_]

Proposed resolution (February, 2017):

  1. Change 7.5.6 [expr.prim.lambda] paragraph 17 as follows:

Every id-expression within the compound-statement of a lambda-expression that is an odr-use (6.3 [basic.def.odr]) of an entity captured by copy is transformed into an access to the corresponding unnamed data member of the closure type. [_Note:_An id-expression that is not an odr-use refers to the original entity, never to a member of the closure type. Furthermore, such an id-expression does not cause the implicit capture of the entity. —_end note_] If *this is captured by copy, each odr-use of this is transformed into a pointer to the corresponding unnamed data member of the closure type, cast (7.6.3 [expr.cast]) to the type of this. [Note: The cast ensures that the transformed expression is a prvalue. —_end note_] An id-expression within the _compound-statement_of a lambda-expression that is an odr-use of a reference captured by reference refers to the entity to which the captured reference is bound and not to the captured reference. [Note: The validity of such captures is determined by the lifetime of the object to which the reference refers, not by the lifetime of the reference itself. —_end note_] [Example:

void f(const int*); void g() { const int N = 10; [=] { int arr[N]; // OK: not an odr-use, refers to automatic variable f(&N); // OK: causes N to be captured; &N points to the // corresponding member of the closure type }; } auto h(int &r) { return [&] { ++r; // Valid after h returns if the lifetime of the // object to which r is bound has not ended }; }

—_end example_]

  1. Change 7.5.6 [expr.prim.lambda] paragraph 25 as follows:

[Note: If an a non-reference entity is implicitly or explicitly captured by reference, invoking the function call operator of the corresponding lambda-expression after the lifetime of the entity has ended is likely to result in undefined behavior. —_end note_]