Date
2017-02-15.00:00:00
Message id
5170

Content

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

Should this be undefined behavior or should it print 5?