是否有任何方法检测混合类型和非类型的任意模板类?

问题描述:

是否有检测的一类是正常类型,或者是模板类型(元类型)其可包括非类型参数任何方式?我想出了这个解决方案:是否有任何方法检测混合类型和非类型的任意模板类?

#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?

+1

可悲[甚至C++ 17不](http://melpon.org/wandbox/permlink/VAUXmYQ6tM5L2GOh)似乎减少所需数量的组合。 –

没有,没有。

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

+0

是的,我知道模板类本身不是类型,而是元类型。尽管如此,人们可以将它们与正常类型区分开来,除非它们采用非类型参数。我认为也许有人可以想出一个办法。 – vsoftco

+0

@vsoft我加2段,因为在你的问题一个小错误(如果你指的模板类为一类)的,但主要是因为第一款规定的时间太短了一个答案。 ;) – Yakk

我想这是不可能的。

无论如何,你可以使用周围的其他方式,让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 

当然,你将无法再使用MixedFooMixedFoo<int, 2>,所以我不知道这是值得的。