【问题标题】:Check whether a type is a template specialization or not [duplicate]检查类型是否是模板特化[重复]
【发布时间】:2015-08-29 12:14:39
【问题描述】:

给定一个任意模板类/结构,就像:

template <typename T>
struct A {};

我想执行&lt;type_traits&gt; 时尚检查(我们将其命名为_A)以确定任意类型是否是A的特化强与否,就像:

#include <type_traits>

template <typename T>
struct is_A: std::integral_constant<bool, /* perform the check here */ > {};

constexpr bool test0 = is_A< A<int> >::value // true
constexpr bool test1 = is_A< A<float> >::value // true
constexpr bool test2 = is_A< int >::value // false
constexpr bool test2 = is_A< float >::value // false

怎么可能?

提前致谢

【问题讨论】:

    标签: c++ templates c++11 template-specialization


    【解决方案1】:

    你需要专攻A&lt;T&gt;

    template <typename T>
    struct is_A : std::false_type { };
    
    template <typename T>
    struct is_A<A<T>> : std::true_type { };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 2019-08-19
      • 2021-09-04
      相关资源
      最近更新 更多