有什么方法可以检测混合类型和非类型的任意模板类?

vso*_*tco 4 c++ templates type-traits c++11 c++14

是否有任何方法可以检测类是普通类型还是可能包含非类型参数的模板类型(元类型)?我想出了这个解决方案:

#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
}
Run Code Online (Sandbox Code Playgroud)

但是对于混合非类型和类型的模板,它将失败,例如

template<class, int> struct MixedFoo{};
Run Code Online (Sandbox Code Playgroud)

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

相关问题(不是伪造):是否可以仅通过标识符来检查成员模板的存在?

Yak*_*ont 5

不,那里没有。

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