mercredi 29 juin 2016

Template partial specialization for multiple types overriding member function

I have class member functions defined as follows, providing a specification for one of them and letting the user provide their own specification for others:

template <typename T>
class Foo {
    // specialization provided for integral types
    template <typename Enable = T>
    typename std::enable_if<std::is_integral<Enable>::value, size_t>::type
        bar(const T& value);

    // provide your own specialization for other types
    template <typename Enable = T>
    typename std::enable_if<!std::is_integral<Enable>::value, size_t>::type
        bar(const T& value);
};

template <typename T>
template <typename Enable>
typename std::enable_if<std::is_integral<Enable>::value, size_t>::type
    Foo<T>::bar(const T& value) {
    ...
}

Now, I'd like to provide a specialization for the function that will work for integral pair types. I have a template metafunction checking for integral pairs defined as follows:

template <typename T>
struct is_integral_pair : std::false_type {}; 

template <typename T1, typename T2> 
struct is_integral_pair<std::pair<T1, T2>> {
    static const bool value =
        std::is_integral<T1>::value && std::is_integral<T2>::value;
};

Is it possible for me to provide my own specialization such that it works for all integer pairs, perhaps using the template metafunction I defined above?

Aucun commentaire:

Enregistrer un commentaire