Bug ID: 37504
           Summary: Lambda should implicitly capture constexpr variable
           Product: clang
           Version: 6.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++14

Created attachment 20314
sample to reproduce the issue

In the example attached, constexpr std::size_t i should be captured with empty
capture list [] but it is not the case in certain conditions. Works fine with
g++ 7.3.0:

constexpr std::size_t i = 1000000;
// this is OK
run([] { std::cout << i << '\n'; });

// this fails to compile
run([/*i*/] {
  std::vector<std::size_t> v(1, i);
  std::cout << v[0] << '\n';

In my real code, I need [i] to make it compile as here but if I write [i], I
get the following warning:
warning: lambda capture 'i' is not required to be captured for this use
which is strange because if I remove i from the capture list, it does not

I do not get this warning with this sample, I did not find how to reproduce it
in a small sample.

I do not know if this is correct but this page:
"A lambda expression can read the value of a variable without capturing it if
the variable
- has const non-volatile integral or enumeration type and has been initialized
with a constant expression, or
- is constexpr and trivially copy constructible."

I have not been able to find where it comes from in the standard. The 2014
standard says in :
"A lambda-expression with an associated capture-default that does not
explicitly capture this or a variable with automatic storage duration (this
excludes any id-expression that has been found to refer to an init-capture’s
associated non-static data member), is said to implicitly capture the entity
(i.e., this or a variable) if the compound-statement:
— odr-uses (3.2) the entity, or
— names the entity in a potentially-evaluated expression (3.2) where the
enclosing full-expression depends on a generic lambda parameter declared within
the reaching scope of the lambda-expression.

But this is unclear to me.

You are receiving this mail because:
You are on the CC list for the bug.
llvm-bugs mailing list

Reply via email to