<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/107018>107018</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang] constexpr template method cannot be used in a constant expression
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Pawday
</td>
</tr>
</table>
<pre>
# [Online example](https://godbolt.org/z/qzaaGfe5h)
```c++
struct ConstexprTemplate
{
static constexpr int sizeof_char()
{
return sizeof_template<char>();
}
static constexpr int sizeof_short()
{
return sizeof_template<short>();
}
static constexpr int sizeof_int()
{
return sizeof_template<int>();
}
static constexpr int sizeof_long()
{
return sizeof_template<long>();
}
template <typename I>
static constexpr I sizeof_template()
{
return sizeof(I);
}
};
constexpr int sizeof_char = ConstexprTemplate::sizeof_char();
constexpr int sizeof_short = ConstexprTemplate::sizeof_short();
constexpr int sizeof_int = ConstexprTemplate::sizeof_int();
constexpr int sizeof_long = ConstexprTemplate::sizeof_long();
```
clang version 18.1.8 (git@github.com:llvm/llvm-project.git 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
Full diagnostic message:
```txt
Example.cc:30:15: error: constexpr variable 'sizeof_char' must be initialized by a constant expression
30 | constexpr int sizeof_char = ConstexprTemplate::sizeof_char();
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Example.cc:5:16: note: undefined function 'sizeof_template<char>' cannot be used in a constant expression
5 | return sizeof_template<char>();
| ^
Example.cc:30:29: note: in call to 'sizeof_char()'
30 | constexpr int sizeof_char = ConstexprTemplate::sizeof_char();
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Example.cc:24:24: note: declared here
24 | static constexpr I sizeof_template()
| ^
Example.cc:31:15: error: constexpr variable 'sizeof_short' must be initialized by a constant expression
31 | constexpr int sizeof_short = ConstexprTemplate::sizeof_short();
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Example.cc:10:16: note: undefined function 'sizeof_template<short>' cannot be used in a constant expression
10 | return sizeof_template<short>();
| ^
Example.cc:31:30: note: in call to 'sizeof_short()'
31 | constexpr int sizeof_short = ConstexprTemplate::sizeof_short();
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Example.cc:24:24: note: declared here
24 | static constexpr I sizeof_template()
| ^
Example.cc:32:15: error: constexpr variable 'sizeof_int' must be initialized by a constant expression
32 | constexpr int sizeof_int = ConstexprTemplate::sizeof_int();
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Example.cc:15:16: note: undefined function 'sizeof_template<int>' cannot be used in a constant expression
15 | return sizeof_template<int>();
| ^
Example.cc:32:28: note: in call to 'sizeof_int()'
32 | constexpr int sizeof_int = ConstexprTemplate::sizeof_int();
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Example.cc:24:24: note: declared here
24 | static constexpr I sizeof_template()
| ^
Example.cc:33:15: error: constexpr variable 'sizeof_long' must be initialized by a constant expression
33 | constexpr int sizeof_long = ConstexprTemplate::sizeof_long();
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Example.cc:20:16: note: undefined function 'sizeof_template<long>' cannot be used in a constant expression
20 | return sizeof_template<long>();
| ^
Example.cc:33:29: note: in call to 'sizeof_long()'
33 | constexpr int sizeof_long = ConstexprTemplate::sizeof_long();
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Example.cc:24:24: note: declared here
24 | static constexpr I sizeof_template()
| ^
4 errors generated.
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMWElv6zYQ_jX0hajBRYrkgw5OHBc5tYfeC1IcyyxoyiWpNMmhv_1BkhdlUbS894InGLIgcIbDb-abRcJ7XViADMW3KN4sRBX2pcv-FP8p8byQpXrOEOMYxbd_WKMtYHgSh6MBFG8QS_chHD3ia8S2iG2LUsnShGXpCsS2L4ht_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