dimanche 6 octobre 2019

How do I automatically generate the == operator with boost/operators.hpp?

Follow up from this since I am asking a slightly different question: What's the difference between using boost::equality_comparable<T> versus overriding bool operator ==?

Here's my attempt of doing it code.

#include <boost/operators.hpp>

enum class AnEnum : uint64_t;

struct Base : boost::equality_comparable<Base, 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. How do I automatically implement the following function:

bool operator == (const Base & lhs, const Base & rhs);

I am using this as a reference: https://www.boost.org/doc/libs/1_71_0/libs/utility/operators.htm#arithmetic

Aucun commentaire:

Enregistrer un commentaire