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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With