ETLCPP / etl

Embedded Template Library
https://www.etlcpp.com
MIT License
2.04k stars 371 forks source link

etl::variant is missing comparison operators #868

Closed relativityspace-jsmith closed 2 months ago

relativityspace-jsmith commented 3 months ago

Hi! Thanks for all the work on this library! We have had good luck using etl::variant (variadic version) on my project. However, today I noticed that it isn't comparable, e.g. comparing two instances of a variant with operator== always produces a compiler error even if the types are comparable. std::variant does implement this feature, and it would be really awesome if etl::variant could as well!

Here's my naive attempt at an implementation (it complies at least):

namespace etl
{
template <class... Types>
constexpr bool operator==(const etl::variant<Types...>& lhs, const etl::variant<Types...>& rhs)
{
    // Are the held types the same?
    if (lhs.index() != rhs.index()) return false;

    // Both valueless?
    if (lhs.valueless_by_exception()) return true;

    // Now check if the contained values compare as equal
    return etl::visit([&](auto const & lhs_downcasted)
    {
        return lhs_downcasted == etl::get<decltype(lhs_downcasted)>(rhs);
    }, lhs);
}
}
jwellbelove commented 3 months ago

Working on that right now!

jwellbelove commented 2 months ago

Fixed 20.38.11