问题
When would I use boost::equality_comparable vs overriding T's bool operator==(T const& rhs)
method?
Here's some sample code.
#include <boost/operators.hpp>
enum class AnEnum : uint64_t;
struct Base : boost::equality_comparable<Base> {
std::shared_ptr<AnEnum > units;
std::shared_ptr<int> value;
bool operator ==(Base const& rhs) {
return (*value == *rhs.value)
&& (*units == *rhs.units);
}
friend bool operator == (const Base & lhs, const Base & rhs) {
return (*lhs.value == *rhs.value)
&& (*lhs.units == *rhs.units);
};
};
I was hoping Boost would auto implement operator == but the compiler complained about a missing implementation error. Why would I want to use boost::equality_comparable when I have to implement it anyways?
来源:https://stackoverflow.com/questions/58243286/whats-the-difference-between-using-boostequality-comparablet-versus-overrid