简体   繁体   English

标准库是否有办法检查两个模板化类型的基本模板类型的相等性?

[英]Does the standard library have a way to check the equality of two templated types' base template type?

Having trouble coming up with the correct terminology to search for this adequately, but does the standard library have something to test for the same base template type?在想出正确的术语来充分搜索时遇到了麻烦,但是标准库是否有一些东西可以测试相同的基本模板类型?

template <typename T>
struct foo {};

template <typename T>
struct bar {};

static_assert(std::is_same_base_type<foo<int>, foo<float>>::value == 1);
static_assert(std::is_same_base_type<foo<int>, bar<int>>::value == 0);

In the standard library?在标准库中?

No, as far I know.不,据我所知。

But is trivial to write it.但是写起来很麻烦。

template <typename, typename>
struct is_same_template : public std::false_type
 { };

template <template <typename> class C, typename T, typename U>
struct is_same_template<C<T>, C<U>> : public std::true_type
 { };

So you can write所以你可以写

static_assert( true == is_same_template<foo<int>, foo<float>>::value, "!" ) ;
static_assert( false == is_same_template<foo<int>, bar<int>>::value, "!" );

The problem of this solution is that the specialization works only for template-template bases receiving only one template type parameter.该解决方案的问题在于,特化仅适用于仅接收一个模板类型参数的模板-模板库。

You can improve it, for bases (template-template arguments) receiving a variadic list of arguments您可以改进它,用于接收可变参数列表的基础(模板模板参数)

template <template <typename...> class C,
          typename ... Ts, typename ... Us>
struct is_same_template<C<Ts...>, C<Us...>> : public std::true_type
 { };

but this doesn't works to check, by examples, std::array但这不适用于通过示例检查std::array

static_assert( true == is_same_template<std::array<int, 3u>,
                                        std::array<float, 5u>>::value, "!" ) ;

For std::array you have to add another specialization对于std::array你必须添加另一个专业化

template <template <typename, std::size_t> class C,
          typename T1, std::size_t S1, typename T2, std::size_t S2>
struct is_same_template<C<T1, S1>, C<T2, S2>> : public std::true_type
 { };

Unfortunately there are innumerable possible template-template signatures so you have to add innumerable is_same_template specializations.不幸的是,有无数可能的模板模板签名,因此您必须添加无数is_same_template

This is the reason (I suppose) there isn't a standard library type-traits.这就是(我想)没有标准库类型特征的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模板类的模板化等式运算符无法编译 - Templated Equality Operator for Template Class Does Not Compile 需要一种在仅标头库中具有任何类型的模板化常量的方法 - Need a way to have templated constants of any type in a header only library 检查基类中派生的模板类是否相等 - Check for equality of derived template classes in the base class C ++标准库是否具有printf转换说明符的模板化getter? - Does the C++ standard library have a templated getter for the printf conversion specifier? Boost或标准库是否提供了检查演员表是否无损的方法? - Does Boost or the Standard Library provide a way to check whether a cast is lossless? 两种标准类型的两种模板功能 - Two template functions for two standard types 具有通用模板化基类型的 STL 容器,接受派生类型 - STL Container with generic templated base type, accepting derived types 是否有标准的 Untokenize 模板化类型? - Is there a Standard Untokenize Templated Type? 你能有一个不同模板类型的模板对象向量吗? - Can you have a vector of templated objects with different template types? 通过基类指针检查模板类是否相等 - Check for template class equality through base class pointer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM