2017-02-16 78 views
0

我有下面的模板类:模板比较运营商

template<int size, typename Type> 
class Matrix { 
    public: 
     Matrix(); 
     ... 
     Type operator()(int row, int column) {...} 
    private: 
     std::array<Type, size*size> _array; 
} 

我想重载equal to比较符来比较Matrix对象:

template <int size, typename LeftType, typename RightType> 
bool operator ==(const Matrix<size, LeftType> & left, const Matrix<size, RightType> & right) { 
    for (int i = 0; i < size; ++i) { 
     for (int j = 0; j < size; ++j) { 
      ... 
     } 
    } 
} 

的问题是,整数类型和现实的比较类型相当不同:

real case

template <int size, typename LeftType, typename RightType> 
bool operator ==(const Matrix<size, LeftType> & left, const Matrix<size, RightType> & right) { 
    for (int i = 0; i < size; ++i) { 
     for (int j = 0; j < size; ++j) { 
      if (qFuzzyIsNull(left(i, j)) || qFuzzyIsNull(right(i, j))) { 
       if (!qFuzzyCompare(left(i, j) + 1, right(i, j) + 1)) { 
        return false; 
       } 
      } else { 
       if (!qFuzzyCompare(left(i, j), right(i, j))) { 
        return false; 
       } 
      } 
     } 
    } 
    return true; 
} 

(我使用Qt的qFuzzyCompareqFuzzyIsNull

integer case

template <int size, typename LeftType, typename RightType> 
bool operator ==(const Matrix<size, LeftType> & left, const Matrix<size, RightType> & right) { 
    for (int i = 0; i < size; ++i) { 
     for (int j = 0; j < size; ++j) { 
      if (left(i, j) != right(i, j)) { 
       return false; 
      } 
     } 
    } 
    return true; 
} 

如何启用integer case如果两个LeftTypeRightType整数,使real case如果至少一个LeftTypeRightType是真实的?

+4

超载'='为'真正type'所以你可以使用两个相同的功能!? – NathanOliver

+0

您可以使用模板特化并针对LeftType和RightType = int编写一个版本。因此,如果两者都是整数,那么总是会调用这个特殊函数。也可以考虑在这种情况下添加明确的内容,如果转换不希望。 – Aeonos

+2

'模板专业化'是你在找什么。但@NathanOliver是正确的,逻辑(嵌套for循环)是相同的,它可能会更好地重载操作符!=真正的类型。 –

回答

1

如何:

template <int size, typename LeftType, typename RightType> 
bool operator ==(const Matrix<size, LeftType> & left, const Matrix<size, RightType> & right) { 
    for (int i = 0; i < size; ++i) { 
     for (int j = 0; j < size; ++j) { 
      if (not is_equal(left(i,j), right(i,j)) { 
       return false; 
      } 
     } 
    } 
    return true; 
} 

,然后你要么定义is_equal几种重载变体或使is_equal模板,定义它的专业化,像

template<class T> 
bool is_equal(const T a, const T b); 

template<> 
bool is_equal<int>(const int a, const int b){ 
    return a == b; 
} 

template<> 
bool is_equal<real>(const real a, const real b){ 
    ... 
} 

(或在两个模板类型如果可能发生)

当然,您可以专门化操作符本身,但这意味着您必须重新编写相同的代码你可能会重复使用它的机会。同时,is_equal可能成为您程序中的一些常用工具。

(注:is_equal是一个有些基本的名字,所以它应该是在一个命名空间,很明显)