vendredi 21 juillet 2017

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?

Aucun commentaire:

Enregistrer un commentaire