<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/57223>57223</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Confusing diagnostics when trying to take the address of a constexpr variable in a constexpr function
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:diagnostics
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
tbaederr
</td>
</tr>
</table>
<pre>
Consider this code:
```c++
constexpr void foo() {
constexpr int m = 10;
constexpr const *int p = &m;
}
```
clang's output when trying to compile this is:
```
./test.cpp:5:24: error: constexpr variable 'p' must be initialized by a constant expression
constexpr const int *p = &m;
^ ~~
./test.cpp:5:24: note: pointer to 'm' is not a constant expression
./test.cpp:3:17: note: address of non-static constexpr variable 'm' may differ on each invocation of the enclosing function; add 'static' to give it a constant address
constexpr int m = 10;
^
static
1 error generated.
```
which is actually _incredibly_ good. GCC just says "`&m` is not a constant expression"...
However, the output also claims that adding `static` to the declaration of `m` will fix things, however:
```
./test.cpp:3:24: error: static variable not permitted in a constexpr function
static constexpr int m = 10;
^
1 error generated.
```
So it really shouldn't claim this if the enclosing function is `constexpr`. If `foo()` is not `constexpr`, things work just fine.
Note: GCC claims that "‘static’ in ‘constexpr’ function only available with ‘-std=c++2b’ or ‘-std=gnu++2b’" - but then doesn't support it when using `-std=c++2b` either. So I'm not sure if GCC is correct here and this maybe actually needs to be implemented or not.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyVVk2TmzgQ_TX4ohoKhG3wwYfJfOzmspf9ASmBGlACEiUJO95fv92CsbFjTyVTwCDU6u73Xqvl0sjT_sVopyRY5lvlWGUkRNlzlLxGycdzm0xXFfEvdIWvFS7z8HOw7GCUZLUxES8ivmNRPlswdrFR2rOeRdkrS5Mo-7J0vzQLbyziz2Q_BPuIb_vLivz1Jqelp6oTuol47pgZ_TB6dmxBM29PSjfMG_TeD6qDCahyF5jX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