Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking whether or not 2 types are comparable

I have the following code to determine whether or not 2 types are comparable.

template<typename T, typename U, typename = std::void_t<>>
struct is_comparable
    : std::false_type
{};

template<typename T, typename U>
struct is_comparable<T, U, std::void_t<decltype((std::declval<T>() == std::declval<U>()))>>
    : std::true_type
{};

Is this an acceptable way of archieving what I am trying to do? Can you see any problems with this design?

EDIT

Keeping cdhowie's comment and Henri Menki's answer in mind, this is how the code looks now.

namespace meta
{
    template<typename T, typename U, typename = std::void_t<>>
    struct has_equal_to_operator
        : std::false_type
    {};

    template<typename R, typename T, typename U, typename = std::void_t<>>
    struct has_equal_to_operator_r
        : std::false_type
    {};

    template<typename T, typename U, typename = std::void_t<>>
    struct has_nothrow_equal_to_operator
        : std::false_type
    {};

    template<typename R, typename T, typename U, typename = std::void_t<>>
    struct has_nothrow_equal_to_operator_r
        : std::false_type
    {};

    template<typename T, typename U>
    struct has_equal_to_operator<T, U, std::void_t<decltype(std::declval<T>() == std::declval<U>())>>
        : std::true_type
    {};

    template<typename R, typename T, typename U>
    struct has_equal_to_operator_r<R, T, U, std::void_t<decltype(std::declval<T>() == std::declval<U>())>>
        : std::is_convertible<decltype(std::declval<T>() == std::declval<U>()), R>
    {};

    template<typename T, typename U>
    struct has_nothrow_equal_to_operator<T, U, std::void_t<decltype(std::declval<T>() == std::declval<U>())>>
        : std::bool_constant<noexcept(std::declval<T>() == std::declval<U>())>
    {};

    template<typename R, typename T, typename U>
    struct has_nothrow_equal_to_operator_r<R, T, U, std::void_t<decltype(std::declval<T>() == std::declval<U>())>>
        : std::bool_constant<(noexcept(std::declval<T>() == std::declval<U>()) && std::is_convertible_v<decltype(std::declval<T>() == std::declval<U>()), R>)>
    {};

    template<typename T, typename U>
    inline constexpr auto has_equal_to_operator_v = has_equal_to_operator<T, U>::value;

    template<typename R, typename T, typename U>
    inline constexpr auto has_equal_to_operator_r_v = has_equal_to_operator_r<R, T, U>::value;

    template<typename T, typename U>
    inline constexpr auto has_nothrow_equal_to_operator_v = has_nothrow_equal_to_operator<T, U>::value;

    template<typename R, typename T, typename U>
    inline constexpr auto has_nothrow_equal_to_operator_r_v = has_nothrow_equal_to_operator_r<R, T, U>::value;
}
like image 201
Yamahari Avatar asked Oct 29 '25 18:10

Yamahari


1 Answers

Here is a solution with void_t like in the question. Additionally I would check if the comparison produces the correct type (bool in this case).

#include <type_traits>
#include <iostream>

template < typename T, typename U >
using equality_comparison_t = decltype(std::declval<T&>() == std::declval<U&>());

template < typename T, typename U, typename = std::void_t<> >
struct is_equality_comparable
  : std::false_type
{};

template < typename T, typename U >
struct is_equality_comparable < T, U, std::void_t< equality_comparison_t<T,U> > >
  : std::is_same< equality_comparison_t<T,U>, bool >
{};

struct X {};

struct Y { int operator==(Y const&) { return 1; } };

int main()
{
  static_assert(false == is_equality_comparable<X, X>(), "!");
  static_assert( true == is_equality_comparable<std::string, std::string>(), "!!");
  static_assert(false == is_equality_comparable<int, std::string>(), "!!!");
  static_assert( true == is_equality_comparable<int, int>(), "!!!!");
  static_assert(false == is_equality_comparable<Y, Y>(), "!!!!!");
}
like image 154
Henri Menke Avatar answered Nov 01 '25 07:11

Henri Menke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!