GCC 4.7.2 compiles this:
constexpr int i = 5;
[]{ std::integral_constant< int, i >(); }; // nonstandard: i not captured
but not this:
constexpr int i = 5;
[&i]{ std::integral_constant< int, i >(); }; // GCC says i not constexpr
The latter example appears correct to me, according to C++11 §5.1.2/15:
An entity is captured by reference if it is implicitly or explicitly captured but not captured by copy. It is unspecified whether additional unnamed non-static data members are declared in the closure type for entities captured by reference.
It seems the captured object i
inside the lambda refers to the variable in the enclosing scope, which is constexpr
, not merely a const
reference.
The standard explicitly says that the use of a by-value capture is transformed into a use of the corresponding member of the lambda object. And I think that 5.1.2 hints that my interpretation is correct.
Is there anything that explicitly says that whether a capture by reference refers to the object in the enclosing scope or a reference?
See Question&Answers more detail:os