有没有办法检测一个类是普通类型还是模板类型(元类型)的实例化可能包含非类型参数 ?我想出了这个解决方案:
#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/39812789/