On Wednesday 22 July 2015 17:19:19 Brian Bi wrote:
> Yet, capturing `this` by reference does not make much sense since `this` is
> a prvalue. The following quotes also from the standard, taken together,
> also seem to imply in a roundabout way that `this` is supposed to always be
> captured by copy
>
> > For each entity captured by copy, an unnamed non-static data member is
> > declared in the closure type.
> >
> > It is unspecified whether additional unnamed non-static data members are
> > declared in the closure type for entities captured by reference.
> >
> > 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 (5.4)
> > to
> > the type of `this`.
>
> Consider this code, that gcc and clang both accept:
>
http://coliru.stacked-crooked.com/a/d1c56761309e6f4a
>
> The standard seems to allow this; there's no rule I can find that says it's
> not allowed.
>
> I wonder whether the intent was that `this` is captured by copy even if the
> *capture-default* is `&`?
I'd say so. I don't think no one is advocating copying the enclosing object.
I'd say the interesting questions would be:
a) is x captured by copy? Or is the this pointer captured? In other words, if
the enclosing object changes the value of x between the lambda creation and
the call, will the returned value update?
b) in a capture defaulting to copy, is the this pointer pointing to a const
object? In other words, is this allowed:
auto l = [] { x++; };
Because if it were called "that" instead of "this", it would be allowed:
void f(S *that)
{
auto l = [that] { that->x++; }
}
--
Thiago Macieira - thiago (AT)
macieira.info - thiago (AT)
kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358