使用std::rel_ops将完整的关系运算符集添加到类的首选方法是什么?
该文档建议一个using namespace std::rel_ops,但是这似乎是有严重缺陷的,因为这将意味着包括以这种方式实现的类的头也将增加全关系运算符所有其他类与定义operator<和operator==,即使是不希望的.这有可能以令人惊讶的方式改变代码的含义.
作为旁注 - 我一直在使用Boost.Operators来做这件事,但我仍然对标准库感到好奇.
我如何获得运营商>,>=,<=,并!=从==和<?
标准头<utility>定义了一个命名空间的std :: rel_ops定义在运营商方面的上述运营商==和<,但我不知道如何使用它(哄我的代码到使用这样的定义为:
std::sort(v.begin(), v.end(), std::greater<MyType>);
Run Code Online (Sandbox Code Playgroud)
我在哪里定义了非成员运营商:
bool operator < (const MyType & lhs, const MyType & rhs);
bool operator == (const MyType & lhs, const MyType & rhs);
Run Code Online (Sandbox Code Playgroud)
如果我#include <utility>并指定using namespace std::rel_ops;编译器仍然抱怨binary '>' : no operator found which takes a left-hand operand of type 'MyType'..