Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 621
Tobias Hermann Avatar asked Jan 30 '17 09:01

Tobias Hermann


1 Answers

You could do it like this:

#include <type_traits>

template <class ... Trest>
struct unique_types;

template <class T1, class T2, class ... Trest>
struct unique_types<T1, T2, Trest ...>
 : unique_types<T1, T2>, unique_types<T1, Trest ...>, unique_types<T2, Trest ...> {};

template <class T1, class T2>
struct unique_types<T1, T2>
{
    static_assert(!std::is_same<T1, T2>::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;
}
like image 182
Jonas Avatar answered Oct 06 '22 01:10

Jonas