Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to require an exact function signature in the detection idiom?

Let's suppose I have a type T and I want to detect whether it has a subscript operator which I can call with with another type Index. The following example works just fine:

#include <type_traits>
#include <vector>

template < typename T, typename Index >
using subscript_t = decltype(std::declval<T>()[std::declval<Index>()]);

int main()
{
    using a = subscript_t< std::vector<int>, size_t >;
    using b = subscript_t< std::vector<int>, int    >;
}

However, I want the function to be detected if and only if the function signature matches exactly. In the example above I would like the statement subscript_t< std::vector<int>, int >; to throw an error like no viable overloaded operator[], because the signature of the subscript operator for std::vector is

std::vector<T, std::allocator<T>>::operator[](size_type pos);

where size_type in GCC is unsigned long. How can I avoid the implicit conversion from int to size_t to take place?

like image 472
Henri Menke Avatar asked Mar 08 '23 13:03

Henri Menke


2 Answers

With is_detected, you may do:

template <typename T, typename Ret, typename Index>
using subscript_t = std::integral_constant<Ret (T::*) (Index), & T::operator[]>;


template <typename T, typename Ret, typename Index>
using has_subscript = is_detected<subscript_t, T, Ret, Index>;

static_assert(has_subscript<std::vector<int>, int&, std::size_t>::value, "!");
static_assert(!has_subscript<std::vector<int>, int&, int>::value, "!");

Demo


I wrote this in SO Documentation:

is_detected

To generalize type_trait creation:based on SFINAE there are experimental traits detected_or, detected_t, is_detected.

With template parameters typename Default, template <typename...> Op and typename ... Args:

  • is_detected: alias of std::true_type or std::false_type depending of the validity of Op<Args...>
  • detected_t: alias of Op<Args...> or nonesuch depending of validity of Op<Args...>.
  • detected_or: alias of a struct with value_t which is is_detected, and type which is Op<Args...> or Default depending of validity of Op<Args...>

which can be implemented using std::void_t for SFINAE as following:

namespace detail {
    template <class Default, class AlwaysVoid,
              template<class...> class Op, class... Args>
    struct detector
    {
        using value_t = std::false_type;
        using type = Default;
    };

    template <class Default, template<class...> class Op, class... Args>
    struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
    {
        using value_t = std::true_type;
        using type = Op<Args...>;
    };

} // namespace detail

// special type to indicate detection failure
struct nonesuch {
    nonesuch() = delete;
    ~nonesuch() = delete;
    nonesuch(nonesuch const&) = delete;
    void operator=(nonesuch const&) = delete;
};

template <template<class...> class Op, class... Args>
using is_detected =
    typename detail::detector<nonesuch, void, Op, Args...>::value_t;

template <template<class...> class Op, class... Args>
using detected_t = typename detail::detector<nonesuch, void, Op, Args...>::type;

template <class Default, template<class...> class Op, class... Args>
using detected_or = detail::detector<Default, void, Op, Args...>;

Traits to detect presence of method can then be simply implemented:

template <typename T, typename ...Ts>
using foo_type = decltype(std::declval<T>().foo(std::declval<Ts>()...));

struct C1 {};

struct C2 {
    int foo(char) const;
};

template <typename T>
using has_foo_char = is_detected<foo_type, T, char>;

static_assert(!has_foo_char<C1>::value, "Unexpected");
static_assert(has_foo_char<C2>::value, "Unexpected");

static_assert(std::is_same<int, detected_t<foo_type, C2, char>>::value,
              "Unexpected");

static_assert(std::is_same<void, // Default
                           detected_or<void, foo_type, C1, char>>::value,
              "Unexpected");
static_assert(std::is_same<int, detected_or<void, foo_type, C2, char>>::value,
              "Unexpected");
like image 84
Jarod42 Avatar answered Apr 07 '23 16:04

Jarod42


You can do this:

template <typename Index, typename ClassType, typename ReturnType>
constexpr bool arg_t(ReturnType (ClassType::*)(Index))
{
    return true;
}

template <typename T, typename Index>
struct subscript_t
{
    static_assert(arg_t<Index>(&T::operator[]));
};

Note that you'll need to instantiate subscript_t, not merely name its type:

int main()
{
    subscript_t< std::vector<int>, size_t > a;
    subscript_t< std::vector<int>, int    > b;
}

Another way, which lets you determine the exact error message:

template <typename ClassType, typename ReturnType, typename ArgType>
constexpr ArgType arg_t(ReturnType (ClassType::*)(ArgType))
{
    return {};
}

template <typename T, typename Index>
struct subscript_t
{
    using Actual = decltype(arg_t(&T::operator[]));
    static_assert(std::is_same<Index, Actual>::value, "oops");
};
like image 25
John Zwinck Avatar answered Apr 07 '23 17:04

John Zwinck