lambda capture scope question

Nathan Sidwell nathan@acm.org
Wed Jan 18 14:46:00 GMT 2017


Jason,
What is the scope of a lambda capture? AFAICT it depends on whether the 
capture is simple or initialized.

the attached program prints:
[x] is i
[x=x] is i
[x=x] {short x;} is s

I'm not sure that's right.

The clearest one is an initialized capture.  [5.1.6]/12 says this is 
equivalent to 'auto VAR = INIT;' 'whose declarative region is the 
lambda-expression's compound statement'. So it looks like '[x=x] is i' 
is right -- we're hiding the lambda's parameter 'x'.

but, if that's the case, is the 3rd one well formed?  Should it be 
treated as an ill-formed redefinition of 'x' in a single scope?  We're 
behaving as-if there's an additional scope just outside the compound 
statement.

For a simple capture nothing appears mentioned about any scope for the 
capture.  We seem to be injecting it just the same as for an initialized 
capture, and therefore hiding the parameter.

IMHO having the scoping rules be different for simple and initialized 
captures is confusing.  I understand Clang behaves differently for this 
example, apparently making the parameter 'x' visible inside the lambda. 
(I have not confirmed that myself)

Perhaps the std wording needs clarifing?

nathan

-- 
Nathan Sidwell
-------------- next part --------------
A non-text attachment was scrubbed...
Name: capture.cc
Type: text/x-c++src
Size: 499 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20170118/535c34dc/attachment.bin>


More information about the Gcc-patches mailing list