是否有任何方法检测混合类型和非类型的任意模板类?
问题描述:
是否有检测的一类是正常类型,或者是模板类型(元类型)其可包括非类型参数任何方式?我想出了这个解决方案:是否有任何方法检测混合类型和非类型的任意模板类?
#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?
答
我想这是不可能的。
无论如何,你可以使用周围的其他方式,让N
推断:
template<class, class> struct MixedFoo;
template<class C, int N> struct MixedFoo<C, std::integral_constant<int, N>>{};
现在,这个返回true
预期:
std::cout << is_template<MixedFoo>() << std::endl; // fails here
当然,你将无法再使用MixedFoo
为MixedFoo<int, 2>
,所以我不知道这是值得的。
可悲[甚至C++ 17不](http://melpon.org/wandbox/permlink/VAUXmYQ6tM5L2GOh)似乎减少所需数量的组合。 –