mercredi 3 février 2016

Declaration of overload operator- of objects with 2 different types

I have a template-class and wrote a operator- which should do the math with 2 objects of different type and return it as a new object. Now i'm wondering how can i declare this operator- in my class, since something like

template<typename S> 
friend auto operator-(const Complex<T>,const Complex<S>)->Complex<decltype(T-S)>; 

isn't allowed? Here is my example code:

template <typename T>
class XY{
private:
    T x

...

// template<typename S> 
// friend auto operator-(const Complex<T>, const Complex<S>)->Complex<decltype(T-S)>;

};

template <typename T, typename S>
auto operator-(const XY<T> z1, const XY<S> z2)->XY<decltype(z1.x - z2.x)>{
    decltype(z1.x - z2.x) x;
    x = (z1.x - z2.x);
    XY<decltype(x)> n(x);

    return n;
}

Aucun commentaire:

Enregistrer un commentaire