<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/102638>102638</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang] Invalid diagnosis "unnamed variable cannot be implicitly captured in a lambda expression" in templated context
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
nitnelave
</td>
</tr>
</table>
<pre>
Minimal reproducer:
```cpp
template <typename T, typename U>
constexpr bool same_as = false;
template <typename T>
constexpr bool same_as<T, T> = true;
template <typename Type, typename... Choices>
concept OneOf = (... || same_as<Type, Choices>);
template <OneOf<int, double> FromType>
double f(FromType);
extern template double f<int>(int);
template <OneOf<int, double> FromType>
double f(const FromType from_value) {
using RtnType = decltype(f(from_value));
[&] {
(void) from_value;
}();
return 0;
}
template double f<int>(int);
```
```bash
$ clang++ -std=c++20 input.cpp
input.cpp:17:12: error: unnamed variable cannot be implicitly captured in a lambda expression
17 | (void) from_value;
| ^
input.cpp:16:7: note: while substituting into a lambda expression here
16 | [&] {
| ^
input.cpp:22:17: note: in instantiation of function template specialization 'f<int>' requested here
22 | template double f<int>(int);
| ^
input.cpp:10:18: note: declared here
10 | double f(FromType);
| ^
1 error generated.
Compiler returned: 1
```
`from_value` is not an unnamed variable (obviously). Somehow, the combination of concepts, forward declaration of template specialization, and the `decltype(f(from_value))` causes the error. Removing any of these elements clears the error.
I could reproduce it from clang 12 all the way to trunk.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVk1v4zYQ_TX0ZRBBomJLOuiQ2GtgD8UC2_a8oMiRxZYiVX44cX99QcqRlTabbIENAskSZ948zrwZkTknTxqxJdtHsj1sWPCDsa2WXqNiZ9x0RlzaX6SWI1NgcbJGBI6WlA8kP5D85brL538-TfMbj-OkmEcg5d5fJtRsRPiN0D0sT7-T8tNszI12Hp8nC50xChwb8RtzQMoD9Ew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