c++ - 有没有办法检测混合类型和非类型的任意模板类?

标签 c++ templates c++11 c++14 typetraits

有没有办法检测一个类是普通类型还是模板类型(元类型)的实例化可能包含非类型参数 ?我想出了这个解决方案:

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

相关文章:

c++ - CoCreateInstance 使用 CLSCTX_LOCAL_SERVER 是否足以确保所有应用程序共享 COM 对象实例?

php - 读取非php文件中的php标签并执行

返回参数的函数模板的 C++ 编译器优化

c++ - 为什么不能在函数中声明模板?

c++ - 将方法的替代实现 (SFINAE) 移动到单独的文件

c++ - 如果线程必须写在同一个 vector 上,是否可以使用线程

c++ - 我的链表反转递归方法代码有什么问题?

c++ - boost 线程,测试线程是否准备好加入

c++ - reprojectImageTo3D 仅创建 NaN 点

c++ - 如何检索模板类的 boost::variant 值