实现两种类型的提升“less_than”
Implementation of two-type version of boost `less_than`
基于 pp130
但是,最好也有一个支持 T 和兼容类型之间的比较,这只是一个案例 添加更多重载。对于对称性,您需要允许任一类型 位于操作的左侧。(这很容易忘记 手动添加运算符;人们往往只能清楚地看到这样一个事实: 右侧必须接受另一种类型。当然,你的两种类型 less_than的版本不会犯这种愚蠢的错误,对吧?
template <class T,class U>
class less_than2
{
public:
friend bool operator<=(const T& lhs,const U& rhs) {
return !(lhs>rhs);
}
friend bool operator>=(const T& lhs,const U& rhs) {
return !(lhs<rhs);
}
friend bool operator>(const U& lhs,const T& rhs) {
return rhs<lhs;
}
friend bool operator<(const U& lhs,const T& rhs) {
return rhs>lhs;
}
friend bool operator<=(const U& lhs,const T& rhs) {
return !(rhs<lhs);
}
friend bool operator>=(const U& lhs,const T& rhs) {
return !(rhs>lhs);
}
};
问>为什么我们不必提供以下两个功能?
friend bool operator>(const T& lhs,const U& rhs) {
return rhs<lhs;
}
friend bool operator<(const T& lhs,const U& rhs) {
return rhs>lhs;
}
此运算符:
friend bool operator>=(const T& lhs,const U& rhs) {
return !(lhs<rhs);
}
取决于函数调用lhr<rhs
的有效性。由您的operator<(const T&, const U&)
第二次提供其签名是没有意义的(甚至是错误的)。
这同样适用于第二次重载。
相关文章:
- 如何使用std::min和std::less返回对象
- std::less是否应该允许在编译时比较不相关的指针?
- 无法获得等效的 std::less 来用于嵌套迭代器
- 如何在长g++错误消息上使用linux less命令
- D-lang being faster than C++?
- MATLAB faster than LAPACK?
- "How to use long long data-type rather than pointers data-type to modify other variables ?"
- enable_if std的组合:: Less sizeof ..使MSVC失败
- 如何实现随机访问迭代器的"less than operator"?
- 如何在不重载"operator()"、"std::less"、"std
- SFINAE 检查 std::less 是否有效
- 如何在 C++ 类中重载 'less than' 运算符以比较常量?
- 为什么我收到错误:"there is more than one default constructor"?
- std::tuple faster than std::array?
- 有没有办法通过'greater/lesser than'而不是"="来定义'int'?
- "compares less than 0"是什么意思?
- "less than" 与GCC 4与6的ifstream比较
- 在 C 族中,在一个循环中,为什么"less than or equal to"比"less than"符号更受欢迎?
- 为什么C++ STL 容器使用 "less than" 运算符<而不是 "equal equal" 运算符== 作为比较器?
- 在评估中使用多个'greater-than / less-than'比较器是否合适?