Is std::equal_to guaranteed to call operator== by default?

本小妞迷上赌 提交于 2019-12-06 07:11:49

Is std::equal_to guaranteed to call operator == by default?

Yes.

If not specialized, equal_to's call operator will invoke operator ==. From Paragraph 20.8.5 of the C++11 Standard:

1 The library provides basic function object classes for all of the comparison operators in the language (5.9, 5.10).

template <class T> struct equal_to 
{
    bool operator()(const T& x, const T& y) const;
    typedef T first_argument_type;
    typedef T second_argument_type;
    typedef bool result_type;
};

2 operator() returns x == y.

std::equal_to is defined as:

template <class T> struct equal_to {
  bool operator()(const T& x, const T& y) const;
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

operator() returns x == y.

So yes, if T is a class type with an operator== overload defined for it as the left operand, it will be used.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!