Jan15
Jan15

Reputation: 549

Check if two template template parameters are the same

Is there a standard (std) way with which I can check two templates, not two template instantiations, for equality?

When I have two template template parameters, which I want to check for equality, ideally, I would like to write

template <template <class> class T1, template <class> class T2>
class Foo{
  static_assert(std::is_same<T1, T2>::value, "T1 and T2 must be same");
};

But can't, because std::is_same takes type template parameters and not template template parameters.

My current "solution" is that I instantiate with a random type (e.g. void) and then check for equality:

template <template <class> class T1, template <class> class T2>
class Foo{
  static_assert(std::is_same<T1<void>, T2<void>>::value, "T1 and T2 must be same");
};

This all nice and well until T1 or T2 cannot be instantiated with my chosen random type (here void).

I guess I could write my own type trait is_same_template, but I kinda want to get around it.

Upvotes: 1

Views: 966

Answers (1)

Rakete1111
Rakete1111

Reputation: 49028

No there is not. But you can easily write your own trait:

template <template <typename...> typename, template <typename...> typename>
struct is_template_same : std::false_type {};

template <template <typename...> typename TT>
struct is_template_same<TT, TT> : std::true_type {};

template <template <typename...> typename TT, template <typename...> typename UU>
inline constexpr bool is_template_same_v = is_template_same<TT, UU>::value;

And then:

static_assert(is_template_same_v<T1, T2>, "T1 and T2 must be the same");

Upvotes: 5

Related Questions