在C++中,编译器/语言是否可以自动推导出未实现的运算符?
例如,如果我有:
class X
{
public:
bool operator ==(const X &x) const;
};有没有办法隐式推导出!=?
我将利用这个问题来回答一个半相关的问题:为什么map的键的唯一要求是实现<运算符?与平等相比又如何呢?
发布于 2009-09-27 03:01:39
STL已经有了一组定义。
在名称空间stl::rel_ops中,可以找到以下定义。
namespace std
{
namespace rel_ops
{
// Uses ==
template <class _Tp> inline bool operator!=(const _Tp& __x, const _Tp& __y);
// Uses <
template <class _Tp> inline bool operator>(const _Tp& __x, const _Tp& __y);
template <class _Tp> inline bool operator<=(const _Tp& __x, const _Tp& __y);
template <class _Tp> inline bool operator>=(const _Tp& __x, const _Tp& __y);
} // namespace rel_ops
}要使用它,您只需执行以下操作:
#include <utility>
using namespace std::rel_ops;不过,就我个人而言,我会尽可能限制其使用范围。
发布于 2009-09-27 01:10:17
关于相关问题,它是一个等价类。两件事'x‘和'y’是等价的,如果
!(x < y) && !(y < x)发布于 2009-09-27 01:11:54
boost operators可以做你想做的事情。
关于你的第二个问题:如果A不小于B且B不小于A,则两个值A和B相等。
https://stackoverflow.com/questions/1482535
复制相似问题