lundi 30 janvier 2017

How to check if every type in a parameter pack is unique?

For a fixed number of template parameters it is easy, although the number of manually written checks grows quadratically.

#include <type_traits>

template <
    typename T1,
    typename T2,
    typename T3,
    typename T4>
struct unique_types
{
    static_assert(!std::is_same<T1, T2>::value, "Types must be unique");
    static_assert(!std::is_same<T1, T3>::value, "Types must be unique");
    static_assert(!std::is_same<T1, T4>::value, "Types must be unique");
    static_assert(!std::is_same<T2, T3>::value, "Types must be unique");
    static_assert(!std::is_same<T2, T4>::value, "Types must be unique");
    static_assert(!std::is_same<T3, T4>::value, "Types must be unique");
};

int main()
{
    // OK.
    unique_types<int, double, char, float> foo;

    // Should not compile.
    unique_types<int, double, char, double> bar;
}

How could this be implemented for a parameter pack with an arbitrary number of types?

Aucun commentaire:

Enregistrer un commentaire