【发布时间】:2017-02-10 06:53:52
【问题描述】:
有什么方法可以检测一个类是普通类型还是模板类型(元类型)的实例可能包含非类型参数?我想出了这个解决方案:
#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?
【问题讨论】:
-
可悲的是not even C++17 似乎减少了所需的组合数量。
标签: c++ templates c++11 c++14 typetraits