llvm / llvm/llvm-project

Presence of trailing return type in generic lambda defined within function template disables constexpr operator()

Open
#174,514 1 comment 0 reactions 0 assignees View on GitHub
clang:frontend constexpr diverges-from:gcc lambda
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

When a generic lambda is defined within a function template and given a trailing return type that is not a _placeholder-type-specifier_, invoking `(lambda)::operator()` is no longer a constant expression. It compiles if the trailing return type is removed or the enclosing function is made into a non-template. Tested with Clang trunk with `-std=c++26`. Compiles on GCC trunk. https://godbolt.org/z/b5qsrrahv

```cpp
template
void foo() {
constexpr auto bar = [](auto) -> int {
return 0;
};

constexpr auto result = bar(0);
}

int main() {
foo();
}
```

Error:

```
error: constexpr variable 'result' must be initialized by a constant expression
8 | [[maybe_unused]] constexpr auto result = bar(0);
note: undefined function 'operator()' cannot be used in a constant expression
8 | [[maybe_unused]] constexpr auto result = bar(0);
```

Contributor guide

Open the contributing guide

Research direction

Start with the minimal reproducer in the issue and compile it with Clang trunk using -std=c++26, then compare behavior with GCC trunk. Trace the handling of the generic lambda's trailing return type inside the function template; done means constexpr auto result = bar(0) is accepted as a constant expression without removing the return type or changing the enclosing function.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.