vendredi 28 août 2015

Template specialization for std::less in C++11, using a template

I have a Matrix class that derives from an Eigen template:

template<typename T,
         int _Rows = Eigen::Dynamic,
         int _Cols = Eigen::Dynamic>
class Matrix : public Eigen::Matrix<T, _Rows, _Cols>

I need to use this type as a key for an std::map container, hence I need a comparator object. I would like to specialize std::less for this purpose. A draft version that does not compile looks like this, to get you the idea:

template<typename Matrix< <typename T,
                           int _Rows = Eigen::Dynamic,
                           int _Cols = Eigen::Dynamic> > >
struct less
{
    bool operator()(const Matrix< <T,
                                   Rows,
                                   Cols>& lhs,
                    const Matrix< <T,
                                   Rows,
                                   Cols>& rhs) const;
    {
      Matrix< <T,
               Rows,
               Cols>::const_iterator lhsIt = lhs.begin();
      Matrix< <T,
               Rows,
               Cols>::const_iterator rhsIt = rhs.begin();
      for (;
           lhsIt != lhs.end();
           ++lhsIt, ++rhsIt)
      {
        if (*lhsIt < *rhsIt)
        {
          return true;
        }
      }
      return false;
    }
};

The problem is that I want to specialize std::less using a template. What is a correct way to code this ? Do I have to resort to template specialization ?

Aucun commentaire:

Enregistrer un commentaire