[4th attempt after several weeks]
Am 25.07.2012 19:03, schrieb
gas...@hotmail.com:
> i was surprised the other day that when u pass references in
> lambda's they actually get copied when capturing by value. If u
> pass a pointer, the pointer gets copied, but not the pointee.
> However if u pass a reference also the thing the reference points
> to get copied. Its the reverse what I would expect (atm) but oth
> the c++ committee often does that. But maybe someone has a powerful
> explanation?
Actually I find the specified behaviour very intuitive: If you capture
by value, this behaves as like copy-initialization of a "by-value"
variable, if you capture by reference, this behaves like a
by-reference parameter of the referenced type. If we decide for either
of these parameter types, this decision does not depend on whether the
argument is a reference or not. In fact, references that enter
expressions *always* behave as if the reference had been removed
first, see [expr] p5:
"If an expression initially has the type “reference to T” (8.3.2,
8.5.3), the type is adjusted to T prior to any further analysis."
In regard to pointers, these are just objects that can be copied, so
they are copied via a = capture as well.
Both behaviours are so important as part of C++ that any different
deduction form would be controversial (I just add as a remark that
capture of "this" is one of the more irritating things).
A similar *analogy* applies to function template deduction by
argument: The = capture corresponds to deducing a parameter type from
this signature form:
template<class T>
void f(T);
This will always determine T to a non-reference type, irrespective of
the value category of the argument or whether the argument is a
reference type or not.
The & capture corresponds to deducing a parameter type from this
signature form:
template<class T>
void f(&T);
and will always bind directly to the referenced argument. Again, this
deduction does not depend on whether the argument is a referenced type
or not.
HTH & Greetings from Bremen,
Daniel Krügler