[llvm-bugs] [Bug 37504] New: Lambda should implicitly capture constexpr variable

via llvm-bugs llvm-bugs at lists.llvm.org
Thu May 17 09:17:21 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=37504

            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
          Assignee: unassignedclangbugs at nondot.org
          Reporter: ufospoke at gmail.com
                CC: llvm-bugs at lists.llvm.org

Created attachment 20314
  --> https://bugs.llvm.org/attachment.cgi?id=20314&action=edit
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
[-Wunused-lambda-capture]
which is strange because if I remove i from the capture list, it does not
compile.

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:
http://en.cppreference.com/w/cpp/language/lambda
says:
"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 5.1.2.12 :
"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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180517/2ee7d160/attachment-0001.html>


More information about the llvm-bugs mailing list