[英]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.