c++ - 检查类是否是模板特化?

标签 c++ templates

<分区>

如何检查给定类型是否是特定类模板的特化?例如,给定

template <class T>
struct A {};

我如何检查 CompareT是一个 A<*>对于某些类型 *在以下内容中:

template<class CompareT>
void compare(){
   // is this A ?
   cout << is_same< A<*> , CompareT >::value;     // A<*> ????
}

int main(){
  compare< A<int> >();
}

例如这里A<int>应该匹配 A<*>并打印 1。

最佳答案

这是您可以提供要匹配的模板的地方:

template <class T, template <class...> class Template>
struct is_specialization : std::false_type {};

template <template <class...> class Template, class... Args>
struct is_specialization<Template<Args...>, Template> : std::true_type {};

static_assert(is_specialization<std::vector<int>, std::vector>{}, "");
static_assert(!is_specialization<std::vector<int>, std::list>{}, "");

关于c++ - 检查类是否是模板特化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31762958/

相关文章:

c++ - std::function模板参数推导

c++ - 作为非模板类​​成员的模板类变量

c++ - 从 value_type 获取 key_type

c++ - 使用 typename::的模板结构特化

java - Java中的成员方法consting?

c++ - 没有电话接线员

python - 使用自定义占位符、对象的点访问和容错来格式化字符串

c++ - 在 C++11 中生成一个随机字符串?

c++ - 制作图形用户界面编辑器

c++ - 每帧在 shared_ptr 中动态分配图像