检查类型是否为模板特化

时间:2015-08-29 12:14:40

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

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

template <typename T>
struct A {};

我想执行<type_traits>时尚检查(让它名为is_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

这怎么可能?

提前致谢

1 个答案:

答案 0 :(得分:3)

您需要专注于A<T>

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

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