<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Lambda should implicitly capture constexpr variable"
href="https://bugs.llvm.org/show_bug.cgi?id=37504">37504</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Lambda should implicitly capture constexpr variable
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>6.0
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>C++14
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>ufospoke@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>Created <span class=""><a href="attachment.cgi?id=20314" name="attach_20314" title="sample to reproduce the issue">attachment 20314</a> <a href="attachment.cgi?id=20314&action=edit" title="sample to reproduce the issue">[details]</a></span>
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:
<a href="http://en.cppreference.com/w/cpp/language/lambda">http://en.cppreference.com/w/cpp/language/lambda</a>
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.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>