相关疑难解决方法(0)

习惯使用std :: rel_ops

使用std::rel_ops将完整的关系运算符集添加到类的首选方法是什么?

文档建议一个using namespace std::rel_ops,但是这似乎是有严重缺陷的,因为这将意味着包括以这种方式实现的类的头也将增加全关系运算符所有其他类与定义operator<operator==,即使是不希望的.这有可能以令人惊讶的方式改变代码的含义.

作为旁注 - 我一直在使用Boost.Operators来做这件事,但我仍然对标准库感到好奇.

c++ idioms c++-standard-library

47
推荐指数
2
解决办法
4313
查看次数

如何使用std :: rel_ops自动提供比较运算符?

我如何获得运营商>,>=,<=,并!===<

标准头<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'..

c++ templates c++11 visual-studio-2012

6
推荐指数
1
解决办法
1604
查看次数