What's the difference between using boost::equality_comparable<T> versus overriding bool operator ==?

喜夏-厌秋 提交于 2020-01-24 00:37:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!