template
struct A
{
A operator%( const T& x);
};
template
A A::operator%( const T& x ) {
You can achieve that simply by adding requires
to restrict the relevant template function:
template // the generic case, no restriction
A operator% ( const Q& right ) const {
return A(std::fmod(x, right));
}
template requires std::is_integral_v && std::is_integral_v
A operator% ( const Q& right ) const {
return A(x % right);
}
The requires
clause gets a constant expression
that evaluates to true
or false
deciding thus whether to consider this method in the overload resolution, if the requires clause is true the method is preferred over another one that has no requires clause, as it is more specialized.
Code: https://godbolt.org/z/SkuvR9