<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/62129>62129</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang rejects valid code with `if constexpr` in a `constexpr` function template
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:frontend
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
sfc-gh-sgiesecke
</td>
</tr>
</table>
<pre>
The following code is accepted by gcc 10.2 and 12.2, but not by clang 16, both with `--std=c++17`:
```
#include <string>
struct A {
static constexpr bool flag = false;
};
template <typename T>
constexpr auto bar() {
if constexpr (T::flag) {
std::string x;
}
return 42;
}
constexpr auto barVar = bar<A>();
```
clang errs with:
```
<source>:19:21: error: variable of non-literal type 'std::string' (aka 'basic_string<char>') cannot be defined in a constexpr function before C++2b
```
I think gcc is correct in accepting this, as the `if constexpr` shouldn't be evaluated in this case.
(Even if `A::flag` were true, the error message would be misleading as this is a `constexpr` function template which isn't required to be `constexpr` for all `A`. In that case, there should only be an error on `constexpr auto barVar = bar<A>();` because `bar<A>` isn't `constexpr`.)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMlEGT6igQxz8NXrpMEdBoDjk4Ola9-9RetwjpJOwguED0zbffauJzdHan9lVZkQTo_vW__6BiNINDbNj6ha0PCzWl0Ycm9no5jMs4GIyo33HR-u6jeRsRem-tvxo3gPYdgomgtMZzwg7aDxi0hpIXApTroBSFYGIP7ZTA-UTz2io3QFnlzz6NcDVpBFbx5TKmjsmDZuKFiZdywyrO5I7xA-M7Gs-_-VVI47SdOgQm9zEF4wYmX2-T-RlTmHSCHbDNy_wFYlLJaNDexYQ_zwFa7y30Vg3A5AF6ZSMyeVvNNofPcX4mPJ2tSjll-jijUyeEt3vaz7BqSh5aFZjYMlE_AACA6R_yM7F9oxLljiCe12YxaGquDn7eaSgM0d1fAqYpOFiJJ_hvoP5QIVdLfHK_I_y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