有没有办法从其完整类型中获取模板类的类型? [英] Is there a way to get type of template class from its complete type?

查看:167
本文介绍了有没有办法从其完整类型中获取模板类的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个用于给定完整类类型的元函数来返回其模板(例如f<foo<bar>>::typef<foo<baz>>::type结果为foo).

I need a meta-function that for given complete class type returns its template (e.g. f<foo<bar>>::type or f<foo<baz>>::type results in foo).

或者它可能在f<foo<bar>, foo<baz>>::value上返回true,在f<foo<bar>, not_foo<baz>>::value上返回false

Or it may return true on f<foo<bar>, foo<baz>>::value and false on f<foo<bar>, not_foo<baz>>::value

P.S:本可以用于许多chrono :: duration之类的类(但适用于重量单位,质量单位等).我需要不同的单位,而不是彼此转换.

P.S: this was meant to be used with many chrono::duration like classes (but for weight units, mass units and so on). I needed different units not to convert one to another.

推荐答案

f<foo<bar>>::type or f<foo<baz>>::type results in foo

不完全是(请参见 is-an-alias -模板考虑等于相同的模板),您可以执行以下操作:

Not exactly (see is-an-alias-template-considered-equal-to-the-same-template), you can do something like:

template <typename T> struct template_class;

template <template <typename> class C, typename T>
struct template_class<C<T>>
{
    template <typename U>
    using type = C<U>;
};

或者它可能在f<foo<bar>, foo<baz>>::value上返回true,在f<foo<bar>, not_foo<baz>>::value

Or it may return true on f<foo<bar>, foo<baz>>::value and false on f<foo<bar>, not_foo<baz>>::value

更容易(即使有限)将专业化为is_same:

It is easier, even if limited, specialization mostly as is_same:

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

template <template<typename> class C, typename T1, typename T2>
struct has_same_template_class<C<T1>, C<T2>> : std::true_type{};

这篇关于有没有办法从其完整类型中获取模板类的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆