是否有标准(std)方法可以检查两个模板,而不是两个模板实例,以获得相等性?
当我有两个模板模板参数,我想检查它是否相等,理想情况下,我想写
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");
};
Run Code Online (Sandbox Code Playgroud)
但不能,因为std::is_same需要类型模板参数而不是模板模板参数.
我当前的"解决方案"是我用随机类型(例如void)进行实例化,然后检查是否相等:
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");
};
Run Code Online (Sandbox Code Playgroud)
这一切都很好,直到T1或T2不能用我选择的随机类型(这里void)实例化.
我想我可以编写自己的类型特征is_same_template,但我有点想绕过它.
不,那里没有.但是你可以轻松编写自己的特质:
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;
Run Code Online (Sandbox Code Playgroud)
然后:
static_assert(is_template_same_v<T1, T2>, "T1 and T2 must be the same");
Run Code Online (Sandbox Code Playgroud)