我想使用 boost operators以这种方式生成比较函数:
template <typename T>
class Foo : boost::less_than_comparable<Foo<T>, Foo<T2> > {
private:
int bar;
public:
template <typename T2>
friend bool operator<(Foo<T> const& f1, Foo<T2> const& f2) {
if (typeid(T) == typeid(T2)) {
return f1.bar < f2.bar;
} else {
return sizeof(T) < sizeof(T2);
}
}
};
这可能吗?如果我们可以以某种方式使用 boost 运算符来生成其余运算符,则该运算符实际上不需要成为 friend 。
最佳答案
你应该只使用一个重载的函数模板:
template <typename T>
bool operator<(Foo<T> const& f1, Foo<T> const& f2) {
return f1.bar < f2.bar;
}
template <typename T1, typename T2>
bool operator<(Foo<T1> const& f1, Foo<T2> const& f2) {
return sizeof(T1) < sizeof(T2);
}
关于c++ - 我可以使用 boost 运算符为类的 2 个对象(具有不同类型)生成比较函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8250298/