<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/73795>73795</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Nested lambda binding reference drops 'const' qualifier on non-const variable
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
aldo-roseman
</td>
</tr>
</table>
<pre>
The following program does not compile in Clang 17, but does in Clang 16: https://godbolt.org/z/rfGfWjrP1
```cpp
#include <type_traits>
#include <concepts>
class simple_class1 {};
class simple_class2_base {};
template <typename T>
concept simple_class2 = std::is_base_of_v<simple_class2_base, T>;
template <typename Func>
void function_with_lambda1(Func&& func) {
return;
}
template <std::invocable<simple_class2_base&> Mapper>
void function_with_lambda2(Mapper&& mapper) {
return;
}
template <typename T> // Removing the template makes it compile
void function_with_template(simple_class1 foo) {
function_with_lambda1([&foo]() {
//function_with_lambda2([&foo]<simple_class2 V>(V const& bar) { // Does not compile
function_with_lambda2([&foo](simple_class2 auto const& bar) { // Does not compile
//function_with_lambda2([&foo](auto const& bar) { // Does compile
(void)foo; // Suppress unused warning
});
});
}
int main() {
simple_class1 foo;
function_with_template<int>(foo);
return 0;
}
```
The error message is (as seen in the godbolt):
```
<source>:23:29: error: binding reference of type 'simple_class1' to value of type 'const simple_class1' drops 'const' qualifier
23 | function_with_lambda1([&foo]() {
| ^~~
<source>:23:27: note: while substituting into a lambda expression here
23 | function_with_lambda1([&foo]() {
| ^
<source>:34:5: note: in instantiation of function template specialization 'function_with_template<int>' requested here
34 | function_with_template<int>(foo);
| ^
1 error generated.
```
It is particularly strange as `foo` is not const.
Note that quite some conditions have to be met for the program to fail compiling:
- The function needs to have a template
- There have to be two nested lambdas with binding references
- The second lambda needs to have specific syntax
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVk9z4rgT_TTi0pWULWEbDhwYCL_6HXZqa3dq9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