samedi 4 juin 2016

Writing a type trait for a binary operator with is_detected

So I have the following code which in my mind is supposed to check whether a type has operator== overloaded. The method works with unary operators, but gives the wrong result for binary ones. Why is that?

    template<typename T, typename U>
    using eq_t = decltype(declval<T&>() == declval<U&>());

    template<typename T, typename U = T>
    struct has_equal_to 
        : is_detected<eq_t, T>::type 
    {};

    template<typename T, typename U = T, typename R = bool>
    struct has_equal_to_exact 
        : is_detected_exact<R, eq_t, T>::type 
    {};

    template<typename T, typename U = T, typename R = bool>
    struct has_equal_to_convertible 
        : is_detected_convertible<R, eq_t, T>::type 
    {};

Then:

struct foo { bool operator==(const foo&) { return true; } };

int main()
{
    std::cout << has_equal_to<foo>::value; // returns false
}

I'm using Clang 3.7.

Aucun commentaire:

Enregistrer un commentaire