<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/94849>94849</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang++] constexpr is not propagated in lambda in a template context under specific condition
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
stephen-hqxu
</td>
</tr>
</table>
<pre>
I initially discovered this issue on the official clang 18 build on Ubuntu. Then I did a quick test on [compiler explorer](https://godbolt.org/), and it seems to affect all clang versions (starting clang 5) that support C++17 constexpr lambda feature; perhaps platform does not really matter.
Consider the following code:
```cpp
template<typename = void>
void doStuff() {
auto equalsOne = [](auto x) -> bool {
return x == 1;
};
static_assert(equalsOne(1), "oops");
}
int main() {
doStuff();
}
```
And compile with:
```sh
clang++ -std=c++17 ./example.cpp -o example
```
This code does not compile and will generate the following errors:
> static assertion expression is not an integral constant expression \
> static_assert(equalsOne(1), "oops");
There are a few observations:
- The code compiles on GCC and MSVC.
- Make `doStuff` non-template, i.e. removing `template<typename = void>`, compiles.
- Change the lambda call signature from `auto x` to `int x`, compiles. However, it still does not compile if it is in C++20 and replace `auto` with explicit lambda template parameter. This basically means it does not compile when lambda is a template, regardless of how it is templated.
- Change the trailing return type of lambda `-> bool` to `-> auto`, or equivalently removing the trailing return type, compiles.
- It does not compile even if decorating the lambda with `constexpr`, i.e. `[](auto x) constexpr -> bool`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVcuO47gO_RplQ8Rw5DzsRRZdSde9vWjMontmO5AtOta0LLklOkn9_YB-JOmqDAYYoCqJHhTJw8NDFaM5OcS92LyIzXGhemp82EfCrkG3bH5e-0Xp9dv-CxhnyChr30CbWPkzBtRAjYlgYuwRvANqEHxdm8ooC5VV7gSrHMreWM3Hv5e9oz6B7w06-ALaaFDwszfVDyCMxFfE5qXybWcsBsBrZ33AIDZHIfOGqI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