c++ - 有没有办法检测混合类型和非类型的任意模板类?

标签 c++ templates c++11 c++14 typetraits

有没有办法检测一个类是普通类型还是模板类型(元类型)可能包含非类型参数的实例化?我想出了这个解决方案:

#include <iostream>

template <template<class...> class> 
constexpr bool is_template()
{
    return true;
}

template <class> 
constexpr bool is_template()
{
    return false;
}

struct Foo{};

template<class> struct TemplateFoo{};

template<class, int> struct MixedFoo{};

int main()
{
     std::cout << std::boolalpha;
     std::cout << is_template<Foo>() << std::endl;  
     std::cout << is_template<TemplateFoo>() << std::endl;  
     // std::cout << is_template<MixedFoo>() << std::endl; // fails here
}

但是对于混合了非类型和类型的模板,它会失败,比如

template<class, int> struct MixedFoo{};

我想不出任何解决方案,除了我必须在重载中明确指定类型的解决方案。当然,由于组合爆炸,这是不合理的。

相关问题(不是骗局):Is it possible to check for existence of member templates just by an identifier?

最佳答案

没有,没有。

请注意,模板类本身并不是类。它们是类的模板。

关于c++ - 有没有办法检测混合类型和非类型的任意模板类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57831115/

相关文章:

c++ - 没有匹配的函数来调用

c++ - 4维隐藏表面去除

c++ - 默认模板参数 - 不必来自对吗?为什么有效?

c++ - 使用 GCC 4.7 从初始化程序列表初始化 unique_ptrs 的容器失败

c++ - 将变量定义为自动限制

c++ - 从文件中读取固定点的固定 double 并转换为长

c++ - cxxfunction 错误消息,内联 R 包

c++ - constexpr 是编译器的 "hint"(如内联)还是 "a binding request"?

c++ - 成员声明中的模板类类型别名替换失败

c++ - 为什么类型推导一次失败,而在两种非常相似的情况下又一次成功?