简体   繁体   English

使用模板在C ++ 11中使用std :: less的模板特化

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

I have a Matrix class that derives from an Eigen template: 我有一个派生自Eigen模板的Matrix类:

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. 我需要使用此类型作为std::map容器的键,因此我需要一个比较器对象。 I would like to specialize std::less for this purpose. 我想专门为此目的专门研究std::less 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. 问题是我想使用模板专门化std::less What is a correct way to code this ? 对此进行编码的正确方法是什么? Do I have to resort to template specialization ? 我是否必须采用模板专业化?

I will also need to specialize std::hash in a similar way to be able to use std::map . 我还需要以类似的方式专门化std::hash以便能够使用std::map

The problem is that I want to specialize std::less using a template. 问题是我想使用模板专门化std::less

Don't. 别。 std::less means "call the < operator" for this class"; specializing it for a class with no < operator is needlessly confusing to others reading your code, and specializing it for a class with a < operator is pointless. std::less意味着“为这个类调用<运算符”“;将它专门用于没有<运算符的类对于其他读取代码的人来说是不必要的混淆,并且专门用于具有<运算符的类是没有意义的。

Just implement a correct operator< overload, and you can use it in std::map . 只需实现一个正确的operator< overload,就可以在std::map使用它。

I will also need to specialize std::hash in a similar way to be able to use std::map . 我还需要以类似的方式专门化std::hash以便能够使用std::map

No, you don't. 不,你没有。 That's only needed for unordered_map . 这只是unordered_map所需要的。

By the way, your comparison algorithm is wrong. 顺便说一下,你的比较算法是错误的。 It reports [2, 1] < [1, 2] and [1, 2] < [2, 1] . 它报告[2, 1] < [1, 2] [1, 2] < [2, 1] Not to mention that it doesn't handle the case when the two matrices have different numbers of elements. 更不用说当两个矩阵具有不同数量的元素时它不处理这种情况。

The correct syntax is 正确的语法是

template <typename T, int Row, int Col>
struct less<Matrix<T, Row, Col>>
{
    bool operator()(const Matrix<T, Row, Col>& lhs,
                    const Matrix<T, Row, Col>& rhs) const
    {
        // implementation:
        return lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
    }
};

which is a specialization. 这是一个专业化。

BTW, your implementation doesn't respect less requirement: (it is not symmetric). 顺便说一下,你的实现并不尊重较少的要求:(它不是对称的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM