C++ 制作一组C++;类具有可比性,同时避免代码中的重复性

C++ 制作一组C++;类具有可比性,同时避免代码中的重复性,c++,templates,macros,non-repetitive,C++,Templates,Macros,Non Repetitive,我需要创建一组类,以便可以相互比较特定类型的实例 我考虑编写一个对每个类都唯一的比较方法,然后在每个类定义中添加以下代码,用我所在类的名称替换T: bool operator== (const T& L, const T& R) {return L.compare(R)==0;} bool operator!= (const T& L, const T& R) {return L.compare(R)!=0;} bool operator&

我需要创建一组类,以便可以相互比较特定类型的实例

我考虑编写一个对每个类都唯一的比较方法,然后在每个类定义中添加以下代码,用我所在类的名称替换T:

bool operator== (const T& L, const T& R)        {return L.compare(R)==0;}
bool operator!= (const T& L, const T& R)      {return L.compare(R)!=0;}
bool operator< (const T& L, const T& R)       {return L.compare(R)<0;}
bool operator<= (const T& L, const T& R)        {return L.compare(R)<=0;}
bool operator> (const T& L, const T& R)       {return L.compare(R)>0;}
bool operator>= (const T& L, const T& R)      {return L.compare(R)>=0;}
bool操作符==(常量T&L,常量T&R){返回L.compare(R)==0;}
布尔运算符!=(const T&L,const T&R){返回L.compare(R)!=0;}
布尔运算符<(常量T&L,常量T&R){返回L.compare(R)=(常量T&L,常量T&R){返回L.compare(R)>=0;}

不过,这有点重复。有没有更通用的方法呢?我想我可以为此编写一个宏,在T上参数化它,但是宏现在不是很cplusplusy,是吗?我还考虑了继承和多态性,从我对它的了解来看(我还没有使用虚拟类;我是C++新手),似乎我会引入不必要的运行时开销,因为我不需要通过基类指针统一访问这些。除了宏或复制粘贴之外,还有更好的方法来实现吗?

我认为最接近的方法是为基类提供运算符,并要求所有可比较的类继承将它们实现为模板化的自由函数并不是一个好主意,因为它将允许编译器在您感兴趣的类型以外的其他类型上尝试该操作

class AClass
{
public:
    int compare(const AClass& a) const
    {
        return this == &a ? 0 : 1; // i'm sure you can do the -1 / 1 stuff
    }

    friend bool operator==(const AClass& L, const AClass& R)
    {
        return L.compare(R) == 0;
    }
};
这将允许比较AClass和所有Decedent

标题
提供了几组类模板(在命名空间
boost
中)。这些模板根据类提供的最少数量的基本运算符定义命名空间范围内的运算符

#包括
#包括
结构整数:boost::完全按顺序排列
{
int值;
整数(intx):值(x){}
};
布尔运算符=a);
断言(a!=b);
}
不使用boost:

#include <cassert>
struct Integer
{
    int value;
    Integer(int x) : value(x) {}
};

bool operator<(Integer lhs,Integer rhs)
{
    return lhs.value < rhs.value;
}
bool operator==(Integer lhs,Integer rhs)
{
    return lhs.value == rhs.value;
}

template< class T >
bool operator!= (const T& L, const T& R) { return !(L==R); }
template< class T >
bool operator<= (const T& L, const T& R) { return (L < R) || (L == R); }
template< class T >
bool operator>  (const T& L, const T& R) { return (!(L < R)) && (!(L == R)); }
template< class T >
bool operator>= (const T& L, const T& R) { return !(L < R); }

int main()
{
    Integer a(1), b(2), c(1);
    assert(a < b);
    assert(a <= b);
    assert(b > a);
    assert(b >= a);
    assert(a == c);
    assert(a != b);
}
#包括
结构整数
{
int值;
整数(intx):值(x){}
};
布尔运算符
布尔运算符!=(常数T&L,常数T&R){return!(L==R);}
模板
布尔运算符
布尔运算符>(常数T&L,常数T&R){return(!(L
布尔运算符>=(常数T&L,常数T&R){return!(L=a);
断言(a==c);
断言(a!=b);
}
或者更接近你原来的问题:

#include <cassert>
struct Integer {
  int value;
  Integer(int x) : value(x) {}
  int compare(const Integer & rhs) const {
    return (value - rhs.value);
  }
};

template< class T >
bool operator== (const T& L, const T& R) { return L.compare(R)==0; }
template< class T >
bool operator!= (const T& L, const T& R) { return L.compare(R)!=0; }
template< class T >
bool operator<  (const T& L, const T& R) { return L.compare(R)<0; }
template< class T >
bool operator<= (const T& L, const T& R) { return L.compare(R)<=0; }
template< class T >
bool operator>  (const T& L, const T& R) { return L.compare(R)>0; }
template< class T >
bool operator>= (const T& L, const T& R) { return L.compare(R)>=0; }

int main() {
  Integer a(1), b(2), c(1);
  assert(a < b);
  assert(a <= b);
  assert(b > a);
  assert(b >= a);
  assert(a == c);
  assert(a != b);
}
#包括
结构整数{
int值;
整数(intx):值(x){}
整数比较(常量整数和rhs)常量{
返回值(值-rhs.值);
}
};
模板
布尔运算符==(常量T&L,常量T&R){返回L.compare(R)==0;}
模板
布尔运算符!=(const T&L,const T&R){返回L.compare(R)!=0;}
模板
布尔运算符<(常量T&L,常量T&R){返回L.compare(R)
布尔运算符(常数T&L,常数T&R){返回L.compare(R)>0;}
模板
布尔运算符>=(常量T&L,常量T&R){返回L.compare(R)>=0;}
int main(){
整数a(1)、b(2)、c(1);
断言(a=a);
断言(a==c);
断言(a!=b);
}

@ TACP,但我不能想出一个好的基于模板的解决方案,所以我想问一下有经验的C++程序员如何真正地完成这个领域。这种方法的缺点是,现在已经为BufftIn和其他类型引入了潜在冲突的运算符(以及错误的使用错误的编译错误的潜在页面)。.相反,我建议引入一个
模板类Compariable
,其中定义了操作符。使用将遵循奇怪的重复模板模式。即Boost为您提供了什么样的操作符库。
#include <cassert>
struct Integer {
  int value;
  Integer(int x) : value(x) {}
  int compare(const Integer & rhs) const {
    return (value - rhs.value);
  }
};

template< class T >
bool operator== (const T& L, const T& R) { return L.compare(R)==0; }
template< class T >
bool operator!= (const T& L, const T& R) { return L.compare(R)!=0; }
template< class T >
bool operator<  (const T& L, const T& R) { return L.compare(R)<0; }
template< class T >
bool operator<= (const T& L, const T& R) { return L.compare(R)<=0; }
template< class T >
bool operator>  (const T& L, const T& R) { return L.compare(R)>0; }
template< class T >
bool operator>= (const T& L, const T& R) { return L.compare(R)>=0; }

int main() {
  Integer a(1), b(2), c(1);
  assert(a < b);
  assert(a <= b);
  assert(b > a);
  assert(b >= a);
  assert(a == c);
  assert(a != b);
}