<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/110857>110857</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Static constexpr method not considered constant expression when templated method argument is a reference
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
spicard12
</td>
</tr>
</table>
<pre>
The following example causes an error in Clang but works with gcc and msvc :
```
enum class LogLevel {
Debug = 0,
Info = 1,
Warning = 2,
Error = 3,
Off = 4,
All = Debug
};
template<LogLevel CompileTimeLevel>
class LogChannel {
public:
static constexpr LogLevel GetCompileTimeLogLevel() { return CompileTimeLevel; }
};
inline LogChannel<LogLevel::Info> MyChannel;
#define LOG(channel, level) \
do \
{ \
if constexpr (level >= channel.GetCompileTimeLogLevel()) \
{ \
cout << "Log enabled\n"; \
} \
} while (false)
template<class TLogChannel>
void PrintChannelName(const TLogChannel& channel) { LOG(channel, LogLevel::Warning); }
int main() {
PrintChannelName(MyChannel);
}
```
Clang reports :
```
<source>:34:66: error: constexpr if condition is not a constant expression
34 | void PrintChannelName(const TLogChannel& channel) { LOG(channel, LogLevel::Warning); }
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
<source>:27:17: note: expanded from macro 'LOG'
27 | if constexpr (level >= channel.GetCompileTimeLogLevel()) \
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:37:5: note: in instantiation of function template specialization 'PrintChannelName<LogChannel<LogLevel::Info>>' requested here
37 | PrintChannelName(MyChannel);
| ^
<source>:34:57: note: function parameter 'channel' with unknown value cannot be used in a constant expression
34 | void PrintChannelName(const TLogChannel& channel) { LOG(channel, LogLevel::Warning); }
| ^
<source>:34:42: note: declared here
34 | void PrintChannelName(const TLogChannel& channel) { LOG(channel, LogLevel::Warning); }
|
```
A more complete example with details can be seen here : https://godbolt.org/z/az4MfMe89
Couldn't validate anywhere whether this error is valid according to the C++ standard or if this is a bug in Clang since gcc and msvc accepts it.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVk-P27YT_TT0ZZCFRFqWffDBK6-DH5D8UqABeqbIkcWGIlWSWic55LMXpLS25XiTHoq2hCFLw3_z3jwOh3uvjgZxS4pHUuwXfAitdVvfK8GdzOmitvLL9mOL0Fit7UmZI-Bn3vUaQfDBowduAJ2zDpSBSnNzhHoIcLLuk4eTCi0chQBuJHT-WQBhO5LtSfbyXGXTL32iGToQmnsP7-zxHT6jBlI-jp2wx3o4AmF7yAitJiMA_M80Npnzmfk37kz0N_bQWc9T8jfa2cz-oWmSdTmz7rRO1rT95Ha5J-zxGkjArtc8IGHV2fPKdr3S-FF1mAyEPY2DzwirlhuDGi4g-6HWSpxZivv7wIMSIKzxAT_37kLNWwzXe0xmQteEbuKS4DAMztxx5BEihFewKKOVwSv_rkBFz9guMk7YE7z_ch4xW4FQJrFJi3x4S-haTKNoBXp0cQOkqF6mbKSdfUbXZ9_ZRjVXBBC61qM22FOMzLT8w4_4uN3y_jYk2wg7BCCsIqwCQuk7ewQ0vNYoSVEZQmli73al_Q2APZxapTG62nDtMXpwXy6jGD5esz3J5NkqCb84ZcLU83_eYWQzEjGbQVdwpniM_He8zwM4nY3o1UwLU_wDdFyZi5AmMd7x5aKAtNRFUveO9_gck4TD3rrgf5IQCKu8HZzAFOkdWxK2W60I2405J75cZDFqRKqgrAHlwdgAfOznJkAcg94ra85niy2BlBX880RDanHvb9eNFE_fbttdImhJ2C6Pj4gSEyGfe24kSmic7aDjwlkgtEz-leddaZl2vW1_7_G6oLsH6JV2P-ARYnENUxlQY0AVT4G2DTSDEen95ViB71EortXXcQyh5XfhTTntJwku_mgJDv8Y0AeU0KLDi3pGLv_qqbiwkv6Lp9clXswie4bXc8c7DBhDVJ4lV47X7GA-GXsy8Mz1EC9nE-VfIwweZWTtP38SfsjIkl4zIlFo7r6Lx7-PYXqbpzK4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