有什么方法可以检测混合类型和非类型的任意模板类? [英] Is there any way of detecting arbitrary template classes that mix types and non-types?

查看:46
本文介绍了有什么方法可以检测混合类型和非类型的任意模板类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检测类是普通类型还是模板类型(元类型)的实例其中可能包含非类型参数?我想出了以下解决方案:

Is there any way of detecting whether a class is a normal type or is an instantiation of a template type (meta type) which may include non-type parameters? I came up with this solution:

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

但是对于混合了非类型和类型,例如

however it will fail for templates that mix non-types and types, like

template<class, int> struct MixedFoo{};

我无法提出任何解决方案,但必须明确指定输入重载。当然,由于组合爆炸,这是不合理的。

I am not able to come up with any solution, except the one in which I must explicitly specify the types in the overloads. Of course this is un-reasonable due to combinatorial explosion.

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

Related question (not a dupe): Is it possible to check for existence of member templates just by an identifier?

推荐答案

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

Note that template classes are not classes themselves. They are templates for classes.

这篇关于有什么方法可以检测混合类型和非类型的任意模板类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆