是否有办法检测混合类型和非类型的任意模板类
Is there any way of detecting arbitrary template classes that mix types and non-types?
是否有任何方法可以检测类是普通类型还是模板类型(元类型)的实例化,其中可能包含非类型参数?我想到了这个解决方案:
#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{};
我不能提出任何解决方案,除非我必须显式地指定重载中的类型。当然,由于组合爆炸,这是不合理的。
相关问题(不是欺骗):是否有可能仅通过标识符检查成员模板的存在?
不,没有。
注意模板类本身并不是类。它们是类的模板
我想这是不可能的。
无论如何,你可以用另一种方法,让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++ - 声明指向返回任何类型并获取任意数量参数的函数的指针
- 类型别名允许分配任意指针,尽管 int* 是必需的
- 包装任意类型/非类型模板类的模板类
- 使用模板将任意类型参数传递给 C++ 中的函数
- 有没有办法找到任意类类型的直接基类(如果有的话)?
- 如何将模运算符与其他数据类型一起使用
- 为什么当我们把变量"u"的数据类型从长整型更改为整型时,模幂会出错?
- is_invocable使用任意函数参数类型
- 如何动态执行具有任意参数类型的函数指针
- 创建一个模板函数来读取 hdf5 中的任意数据类型
- 如何根据特定类型任意启用或禁用类方法
- C++任意整数(基本类型)的随机数
- C++11 中任意类型 T 的最小值/最大值
- C/C++对一块内存进行malloc,然后对不同的任意类型使用不同的部分
- 使用基元类型进行模乘法的方法
- 将任意数量的任意类型的值组合为单个字符串的简单命令
- 递归类型真的是构建不连续的任意大小数据结构的唯一方法吗
- 在一组模板参数中插入任意数量的类型
- protobuf 3中的基元类型任意