Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Message-ID | <imtf1t$i7k$1@dont-email.me> (permalink) |
|---|---|
| Newsgroups | comp.std.c++ |
| From | Daniel Krügler <daniel.kruegler@googlemail.com> |
| Subject | Re: C++0x: why is capturing (w.r.t. lambdas) restricted to auto variables? |
| Organization | A noiseless patient Spider |
| References | <935b5058-ca67-4720-9391-98a53b145dd7@q14g2000vbf.googlegroups.com> <72ed72d9-8a66-4d00-86ff-6c1cef46798c@d19g2000yql.googlegroups.com> |
| Date | 2011-03-30 13:06 -0600 |
Am 29.03.2011 21:10, schrieb SG:
>
> In case it wasn't clear what I was asking about two weeks ago, here is
> a rewording including two other examples:
I guess that there was no response yet, because of the Madrid meeting
(including preparation time for this event).
> On 15 Mrz., 18:36, SG wrote:
>>
>> I just checked 5.1.2/7-10 [expr.prim.lambda] and noticed something
>> that looks like an unnecessary restriction to me:
>>
>> N3225.pdf, 5.1.2/10:
>> "The identifiers in a capture-list are looked up using the usual
>> rules for unqualified name lookup (3.4.1); each such lookup
>> shall find a variable with automatic storage duration declared
>> in the reaching scope of the local lambda expression. An entity
>> (i.e. a variable or this) is said to be explicitly captured if
>> it appears in the lambda-expression=92s capture-list."
>>
>> Apparently, the following code
>>
>> struct S1 {
>> int x;
>> void f() {
>> [x]() mutable ->int {
>> ++x;
>> return x;
>> }();
>> }
>> };
>>
>> violates 5.1.2/10 because 'x' is not (necessarily) a variable with
>> automatic storage duraction.
Yes, this fact has already been observed by others, e.g. by Dave
Abrahams around June 2010 elsewhere and by Helmut Jarausch (End of
February this year) on c.l.c.mod.
> Another example would be
>
> int global = 0;
> int main() {
> [global]{return global;}();
> }
>
> Basically, I would appreciate someone explaining whether the above
> examples are supposed to work or not. It is my interpretation that
> they are not supposed to work. Please confirm or correct me if I'm
> wrong.
Your interpretation is correct. The committee decided intentionally to
limit the possibilities of captures in this first release of lambdas
in C++. It looks quite reasonably to allow for (explicit) capturing
non-automatic variables in the future as well. The original lambda
proposal also allowed for capturing expressions, but that was
considered as going too far for this initial release.
> Another aspect is: What happends w.r.t. capturing local references?
>
> int main() {
> int i = 0;
> int& r = i;
> bool question = [r]{return&r;}() ==&i;
> }
>
> Does the lambda object store "a copy of a reference" or "a copy of an
> int"? I could not find anything conclusive in the current draft. GCC
> seems to copy the int (which is a desirable behaviour, IMHO).
There does not exist the concept of a "copy of a reference". IMO the
wording in p. 14 clearly says that the referenced object is copied in
this case:
"The type of such a data member is the type of the corresponding
captured entity if the entity is not a reference to an object, or the
referenced type otherwise."
HTH & Greetings from Bremen,
Daniel Krügler
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to comp.std.c++ | Previous | Next — Previous in thread | Next in thread | Find similar
Re: C++0x: why is capturing (w.r.t. lambdas) restricted to auto variables? SG <s.gesemann@gmail.com> - 2011-03-29 13:10 -0600 Re: C++0x: why is capturing (w.r.t. lambdas) restricted to auto variables? Daniel Krügler <daniel.kruegler@googlemail.com> - 2011-03-30 13:06 -0600 Re: C++0x: why is capturing (w.r.t. lambdas) restricted to auto variables? "Johannes Schaub (litb)" <schaub.johannes@googlemail.com> - 2011-03-30 13:05 -0600
csiph-web