I’ve got a problem with overloading some operators.
Specifically, I have a template, interface_mixin<T>, which has the traditional CRTP setup, i.e., Derived : public interface_mixin<Derived>.
Now I need to overload operators on which side is an object, but the other side is a template, that is:
template<typename T, typename Derived> ... operator..
(T t, const interface_mixin<Derived>& d) {
...
}
template<typename T, typename Derived> ... operator..
(const interface_mixin<T>& t, Derived d) {
...
}
template<typename T, typename Derived> ... operator..
(const interface_mixin<T>& t, const interface_mixin<Derived>& d) {
...
}
However, my compiler (VS2010) won’t accept this, calling ambiguous overload. How can I convince it to accept these overloads?
Right now, I’m trying to use SFINAE to try to clear the other overloads. But even though the logic seems fine, the compiler picks the wrong overload.
template<typename T, typename Derived>
typename std::enable_if<
!std::is_base_of<
interface_mixin<T>,
T
>::value,
and<
equality_rule<T>,
Derived
>
>::type operator>>(T t, const interface_mixin<Derived>& d) {
return and<equality_rule<T>, Derived>(equality_rule<T>(std::move(t)), d.crtp_cast());
}
template<typename T, typename Derived>
typename std::enable_if<
!std::is_base_of<
interface_mixin<Derived>,
Derived
>::value,
and<
T,
equality_rule<Derived>
>
>::type operator>>(const interface_mixin<T>& t, Derived d) {
return and<T, equality_rule<Derived>>(t.crtp_cast(), equality_rule<Derived>(std::move(d)));
}
template<typename T, typename Derived> and<T, Derived> operator>>(const interface_mixin<T>& t, const interface_mixin<Derived>& d) {
return and<T, Derived>(t.crtp_cast(), d.crtp_cast());
}
However, VS is picking the wrong overload, and the logic won’t make sense when the wrong overload is picked.
The first part of your question differs form second, you don’t specify return type an function body there.
If return type is
void– this code works with my MSVC2010 compiler:I don’t know if the problem is with return type or something other, but
enable_ifshould solve this problem.