The following code compiles with MSVC (/std:c++latest) and GCC (-std=c++2c), but not Clang (-std=c++2c). Compiler Explorer demo here.
#include <type_traits>
struct A
{
A(int set_value) : _value{set_value}
{
}
template <typename other>
requires std::is_arithmetic<other>::value
operator other() const
{
return other(_value);
}
A operator*(const A &rhs) const
{
return _value * rhs._value;
}
int _value = 0;
};
int main()
{
(void)(A(3) * -1);
}
Which compiler(s) are handling this code correctly? What is the preferred way to get this working under Clang?
Clang's output from Compiler Explorer:
<source>:26:17: error: use of overloaded operator '*' is ambiguous (with operand types 'A' and 'int')
26 | (void)(A(3) * -1);
| ~~~~ ^ ~~
<source>:16:7: note: candidate function
16 | A operator*(const A &rhs) const
| ^
<source>:26:17: note: built-in candidate operator*(float, int)
26 | (void)(A(3) * -1);
| ^
<source>:26:17: note: built-in candidate operator*(double, int)
<source>:26:17: note: built-in candidate operator*(long double, int)
<source>:26:17: note: built-in candidate operator*(int, int)
[...many similar lines omitted...]
<source>:26:17: note: built-in candidate operator*(unsigned long long, unsigned long)
<source>:26:17: note: built-in candidate operator*(unsigned long long, unsigned long long)
<source>:26:17: note: built-in candidate operator*(unsigned long long, unsigned __int128)
1 error generated.
Compiler returned: 1
(I am trying to create a strong_alias wrapper type which is interchangeable with primitive types, but not other strong_aliases.)
explicit constexpr operator other() const;andexplicit constexpr strong_alias(other const&);