对于给定的类,如果我想编写所有比较运算符,为避免代码重复,我会像这样编写它们:
class B {
public:
bool operator==(Type const& rhs) const {
return as_tuple() == rhs.as_tuple();
}
bool operator!=(Type const& rhs) const {
return as_tuple() != rhs.as_tuple();
}
// .. and same for other operators ..
private:
auto as_tuple() const {
return std::tie(a, b, c); // all the members
}
};
我可以使用 std::tie()
或 std::forward_as_tuple()
来实现 as_tuple()
,这两者有区别吗?我应该选择哪一个呢?
tie
和forward_as_tuple
之间的区别是什么?”,你能不能把这个作为问题? - Barrystd::tie()
函数的代码能够编译吗?由于方法是const,但是tie
会生成左值引用。 - Slava