作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要一个元函数,对于给定的完整类类型返回其模板(例如 f<foo<bar>>::type
或 f<foo<baz>>::type
结果为 foo
)。
或者它可能会返回true
上f<foo<bar>, foo<baz>>::value
和false
上f<foo<bar>, not_foo<baz>>::value
P.S:这应该与许多 chrono::duration 之类的类一起使用(但对于重量单位、质量单位等)。我需要不同的单位才能将一种单位转换为另一种单位。
最佳答案
f<foo<bar>>::type or f<foo<baz>>::type results in foo
不完全是(参见 is-an-alias-template-considered-equal-to-the-same-template ),您可以执行以下操作:
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>;
};
Or it may return true on
f<foo<bar>, foo<baz>>::value
and false onf<foo<bar>, not_foo<baz>>::value
即使有限,特化也更容易,主要是 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{};
关于c++ - 有没有办法从模板类的完整类型中获取其类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61278669/
我是一名优秀的程序员,十分优秀!