std :: equal_to是否保证默认调用operator ==?

Mar*_*som 2 c++ std comparison-operators

我一直认为标准需要非专业模板std::equal_to<T>才能调用T::operator==,但我注意到cppreference.com上描述几乎意味着它是另一种方式; 当然它没有提到它作为一项要求.我还检查了C++ 11草案标准N3337,但也找不到任何保证.

如果您创建一个类,operator==您希望它可以在所有情况下使用.

我不能老实地想到一种实现方式不会这样做std::equal_to,但我错过了什么?

And*_*owl 7

std :: equal_to是否保证operator ==默认调用?

是的.

如果不是专门的,equal_to调用操作符将调用operator ==.从C++ 11标准的第20.8.5段开始:

1该库为语言中的所有比较运算符提供基本的函数对象类(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;
};
Run Code Online (Sandbox Code Playgroud)

2 operator()返回x == y.