joaocandre
joaocandre

Reputation: 1745

How to specialize templated operator overloads?

I'm trying to overload comparison operators as non-members for a particular templated class sub, 1) between instances of sub, and 2) between sub and a specific variable, which returns an instance of either comparer_sub for the first case and comparer_el in the alternative, which perform comparisons on sub along with some other useful members:

template <typename T1, typename T2>
class sub_base {
public:
    sub_base() {};
};

template <typename T>
class mat {
    class sub : public sub_base<T,mat<T>> {
    public:
        sub(): sub_base<T,mat<T>>() {};
    };
public:
    int mb;
    sub getSub() {return sub();};
};

template <typename T1,typename T2>
class comparer_base {
public:
    comparer_base() {};
    void base_method() {};
};

template <typename lT1,typename lT2,typename rT1,typename rT2>
class comparer_sub :  public comparer_base<sub_base<lT1,lT2>,sub_base<rT1,rT2>> {
    using comparer_base<sub_base<lT1,lT2>,sub_base<rT1,rT2>>::base_method;
public:
    comparer_sub() : comparer_base<sub_base<lT1,lT2>,sub_base<rT1,rT2>>() {};
};

template <typename lT1,typename lT2,typename rT>
class comparer_el :  public comparer_base<sub_base<lT1,lT2>,rT> {
    using comparer_base<sub_base<lT1,lT2>,rT>::base_method;
public:
    comparer_el() : comparer_base<sub_base<lT1,lT2>,rT>() {};
};

template <typename lT1,typename lT2,typename rT1,typename rT2>
comparer_sub<lT1,lT2,rT1,rT2> operator== (const sub_base<lT1,lT2>& lhs, const sub_base<rT1,rT2>& rhs){
    printf("comparer_sub\n");
    return comparer_sub<lT1,lT2,rT1,rT2>();
};
template <typename lT1,typename lT2,typename rT>
comparer_el<lT1,lT2,rT> operator== (const sub_base<lT1,lT2>& lhs, const rT& rhs){
    printf("comparer_el\n");
    return comparer_el<lT1,lT2,rT>();
};

However, any type of comparison I try to perform the second overload is called, what I assume is due to const rt& being too generic and any instance of sub fits that argument.

int main(int argc, char const *argv[])  {
    mat<int>     A;
    mat<float>  B;
    float C = 0.6;

    A.getSub() == B.getSub(); // comparer_el
    A.getSub() == C; // comparer_el

    return 0;
}

How can I force the first overload to be prioritized over the second overload between instances of sub??

Upvotes: 1

Views: 71

Answers (1)

user1810087
user1810087

Reputation: 5334

The problem is, comparer_el with rT=sub is a better choise than the substitution+cast from sub to sub_base<rT1,rT2>. Deduction does not implicitly cast (except putting additional c/v qualifiers)... For a much better description, read this answer.

So, one solution is not to use return type sub within getSub() but the base type sub_base<rT1,rT2>. see here

important part:

template <typename T>
class mat {
    class sub : public sub_base<T,mat<T>> {
    public:
        sub(): sub_base<T,mat<T>>() {};
    };
public:
    int mb;
    sub_base<T,mat<T>> getSub() {return sub();};
 // ^^^^^^^^^^^^^^^^^^ return type changed from sub to sub_base<T,mat<T>>
};

Upvotes: 1

Related Questions