<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/126855>126855</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Clang++] C enum incorrectly specializes into concept for operator<< overload
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Sicryll
</td>
</tr>
</table>
<pre>
When attempting to resolve a shift operation using a RHS C style enum value, if there is an operator overload for `<<` which is templated by a concept the compiler will erroneously attempt to specialize the function and produce the following error:
```
overloaded 'operator<<' must have at least one parameter of class or enumeration type
```
This can be observed in the example below or via [https://godbolt.org/z/oo5n4xPqa](https://godbolt.org/z/oo5n4xPqa):
```
#include <concepts>
enum { shift = 1 };
template<typename T>
concept AnyConcept = requires
{
{ T::Function() };
};
template<AnyConcept T>
inline T& operator<<(T& t, char value)
{
return t;
}
int main()
{
return 1 << shift;
}
```
This error can be reproduced in all mainline versions of clang that support concepts available in compiler explorer, going back through to 11.0. Both MSVC and GCC produce correct results and do not mirror this error.
A workaround is possible for the issue by using `enable_if` as below:
```
template<AnyConcept T, typename std::enable_if_t<AnyConcept<T>>>
inline T& operator<<(T& t, char value)
{
return t;
}
```
The issue is observed when the second parameter of the `operator<<` overload is any inbuilt numeric type that can be implicitly converted to from the enum value (ex. `int`, `float`). In my testing the issue was observable with all variations of enum declarations that resulted in the values being promoted to the parent namespace, the issue did not occur when using `enum struct` or `enum class`.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykVsGO4zYM_RrlQmzgyOPEPuSQeJq2hwJFd9EeF7LMxOrKoleSk8l-fUHZSWZmt0CBAgJmoojkI_n4GBWCOTnErSj2onheqDF25LcfjfZXaxcNtdftXx06UDFiP0TjThAJPAayZwQFoTPHCDSgV9GQgzHwEwV__PIRagjxahHQjT2clR1RyBrMEWKHHsEEUG42JQ90Rm9JtXAkD2Kdibzms87g0hnd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