[cfe-dev] [EXTERNAL] Issue where Lambda capture of reference to globals doesn't actually capture anything

Richard Smith via cfe-dev cfe-dev at lists.llvm.org
Mon Oct 26 17:14:27 PDT 2020


On Mon, 26 Oct 2020 at 16:57, Lewis, Cannada via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> I’m not a standards reading expert but does
>
> *Note 7 <https://eel.is/c++draft/expr.prim.lambda#capture-note-7>*:
> An *id-expression*
> <https://eel.is/c++draft/expr.prim.id.general#nt:id-expression> that is
> not an odr-use refers to the original entity, never to a member of the
> closure type.
> <https://eel.is/c++draft/expr.prim.lambda#capture-11.sentence-2>
> However, such an *id-expression*
> <https://eel.is/c++draft/expr.prim.id.general#nt:id-expression> can still
> cause the implicit capture of the entity.
> <https://eel.is/c++draft/expr.prim.lambda#capture-11.sentence-3>
> — *end note*
>
> From the text imply that the program https://godbolt.org/z/feKxdK is
> actually implementation defined? Or does gcc have a bug here?
>

GCC has a bug, according to the standard wording. The mention of myfoo does
not constitute an odr-use, so is not rewritten to use the capture. Clang's
behavior is correct per the standard wording.

The standard rule is certainly surprising in this particular case. I think
the rule in question is driven by the desire for adding a capture-default
to a valid lambda to not change its meaning. For example:
https://godbolt.org/z/nrWsvj

-Drew
>
> On Oct 26, 2020, at 4:33 PM, Hubert Tong <hubert.reinterpretcast at gmail.com>
> wrote:
>
> On Mon, Oct 26, 2020 at 1:26 PM Lewis, Cannada via cfe-dev <
> cfe-dev at lists.llvm.org> wrote:
>
>> Hello,
>>
>> We recently encountered an issue where clang has some unexpected behavior
>> with respect to the capture of local references to global variables
>> https://godbolt.org/z/KasP9K.
>>
>> Most compilers (gcc, MSVC, icc) appear to create a member variable to
>> hold the value of `myfoo` and have `dummy` return a size of 16.  Clang does
>> not store a member for `myfoo` (in the AST there is no `FieldDecl` for
>> `myfoo`).
>>
>> This leads to the interesting issue here: https://godbolt.org/z/G59e7M
>> Where clang and gcc will print out different values.
>>
>> I don’t know if this is a clang issue (known?), or a {gcc,icc,msvc}
>> issue, or is implementation defined, but any insight on this would be
>> welcome.
>>
> N4861 subclause 7.5.5.2 [expr.prim.lambda.capture] paragraph 11 seems to
> apply. The local entity being captured is a reference and it is not
> odr-used (see [basic.def.odr] paragraph 4).
>
>
>>
>> -Drew
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at lists.llvm.org
>> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20201026/50eb7bc1/attachment-0001.html>


More information about the cfe-dev mailing list