llvm / llvm/llvm-project

Lambda evaluated for constant template parameter of function inside templated generic lambda incorrectly complains about local variables needing captures

Open
#163,837 3 comments 0 reactions 1 assignee Claimed by @Backl1ght View on GitHub
clang:frontend confirmed lambda
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

This is a reduced example :

```c++
// -std=c++20 -fsyntax-only
template
void f();

template
struct X {
using type = decltype([](auto) {
f<[]{
int result = 0;
return result;
}()>();
}(0));
};

X l;
```

```
:9:20: error: variable 'result' cannot be implicitly captured in a lambda with no capture-default specified
9 | return result;
| ^
:7:13: note: while substituting into a lambda expression here
7 | f<[]{
| ^
:6:27: note: in instantiation of function template specialization 'X::(anonymous class)::operator()' requested here
6 | using type = decltype([](auto) {
| ^
:14:9: note: in instantiation of template class 'X' requested here
14 | X l;
| ^
:8:17: note: 'result' declared here
8 | int result = 0;
| ^
:7:11: note: lambda expression begins here
7 | f<[]{
| ^
:7:12: note: capture 'result' by value
7 | f<[]{
| ^
| result
:7:12: note: capture 'result' by reference
7 | f<[]{
| ^
| &result
:7:12: note: default capture by value
7 | f<[]{
| ^
| =
:7:12: note: default capture by reference
7 | f<[]{
| ^
| &
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.