C++ 检查两个模板参数是否相同

C++ 检查两个模板参数是否相同,c++,C++,是否有一种标准(std)方法可以检查两个模板,而不是两个模板实例是否相等 当我有两个模板参数时,我想检查它们是否相等,理想情况下,我想编写 这一切都很好,直到T1或T2不能用我选择的随机类型实例化(这里是void) 我想我可以写我自己的类型trait是相同的模板,但我有点想绕过它。不,没有。但你可以很容易地写出自己的特点: template <template <typename...> typename, template <typename...> typen

是否有一种标准(
std
)方法可以检查两个模板,而不是两个模板实例是否相等

当我有两个模板参数时,我想检查它们是否相等,理想情况下,我想编写

这一切都很好,直到
T1
T2
不能用我选择的随机类型实例化(这里是
void


我想我可以写我自己的类型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;
模板
结构与模板相同:std::false\u类型{};
样板
struct是_template_same:std::true_type{};
样板
inline constexpr bool is_template_same_v=is_template_same::value;
然后:

static_assert(is_template_same_v<T1, T2>, "T1 and T2 must be the same");
静态断言(模板是否相同?“T1和T2必须相同”);

不,没有。但你可以很容易地写出自己的特点:

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;
模板
结构与模板相同:std::false\u类型{};
样板
struct是_template_same:std::true_type{};
样板
inline constexpr bool is_template_same_v=is_template_same::value;
然后:

static_assert(is_template_same_v<T1, T2>, "T1 and T2 must be the same");
静态断言(模板是否相同?“T1和T2必须相同”);

这是一个很好的答案,但它不包括模板具有非类型参数的情况。@FrançoisAndrieux,它也不会处理混合类型和非类型。和模板参数。像是真的,应该有某种“元”参数来避免所有这些。@ RaKeE111哈哈,我同意,C++是那样丑陋的。您甚至不能让类型模板和非类型模板具有相同的名称。@Jan15我想将它们称为模板参数,但该名称已被采用:(@Rakete1111 auto-template-parameter?这是一个很好的答案,但它不包括模板具有非类型参数的情况。@FrançoisAndrieux,它也不会处理混合类型和非类型。以及模板-模板参数。就像真的一样,应该有某种“meta”参数,以避免所有这些。@ RaKeE111哈哈,我同意,C++是丑陋的,在那种方式下,你甚至不能有类型和非类型模板具有相同的名称。@ JAN15我想称之为模板模板参数,但已经取了这个名字:(@ RaKeE1111自动模板参数?