如何在可变类型包中获取类型的索引
How to get the index of a type in a variadic type pack?
例如
template<typename T, typename... Ts>
struct Index
{
enum {value = ???}
};
假设T是Ts中的一个,并且Ts有不同的类型,比如
Index<int, int, double>::value is 0
Index<double, int, double>::value is 1
#include <type_traits>
#include <cstddef>
template <typename T, typename... Ts>
struct Index;
template <typename T, typename... Ts>
struct Index<T, T, Ts...> : std::integral_constant<std::size_t, 0> {};
template <typename T, typename U, typename... Ts>
struct Index<T, U, Ts...> : std::integral_constant<std::size_t, 1 + Index<T, Ts...>::value> {};
您可能想要添加一个c++14变量模板:
template <typename T, typename... Ts>
constexpr std::size_t Index_v = Index<T, Ts...>::value;
DEMO
相关文章:
- 参数包中的筛选器类型
- 实例化模板时,我是否必须显式显示参数包中的类型?
- 如何检测参数包中的uint8_t类型
- 如何检查参数包是否具有执行顺序中的确切类型
- 从 Any 包中提取和匹配 protobuf 消息类型名的首选方法
- 将函数应用于元组中的每个元素,将每个元素强制转换为类型包中的不同类型,然后作为参数包传递
- 了解元函数以在类型包中查找类型
- 是否可以使用 Boost.Hana 将 std::array 解压缩到非类型模板参数包中
- 如何从模板类的可变参数包中的每个类型恢复非类型名模板参数?
- 在闭包中,如何通过存储在内存中的指针或引用类型捕获可变性或用现代函数式语言进行处理?
- 通过参考推断模板包中的冲突类型
- 如何检查参数包中的每个类型是否唯一
- 在参数包中获取函数指针的返回类型,并将其保存为与其他参数连接的元组
- 从参数包中提取成员类型
- 使用从外部参数包中获取的参数类型声明函数
- 类型 traits,用于检查参数包中的所有类型是否都是可复制构造的
- 从包中删除某个类型的某些(但不是全部)匹配项
- 如何从可变类型包中获取特定类型
- 正在获取参数包中的参数类型
- 如何在可变参数包中找到"min"类型?