C++(14):获取类型在tuple中的索引

tuple没有提供方法可以获取某个类型在tuple中的索引,借助模板推到,我们可以自己实现一个:

#include <iostream>
#include <tuple>

using namespace std;

template<class T, size_t N, class... Args>
struct get_index_of_element_from_tuple_by_type
{
    static constexpr auto value = N;
};

template<class T, size_t N, class... Args>
struct get_index_of_element_from_tuple_by_type<T, N, T, Args...>    //T与tuple中要查找的T匹配
{
    static constexpr auto value = N;
};

template<class T, size_t N, class U, class... Args>
struct get_index_of_element_from_tuple_by_type<T, N, U, Args...>    //T与tuple的第一个类型U不匹配,弹出U,N+1,递归调用get_index_of_element_from_tuple_by_type
{
    static constexpr auto value = get_index_of_element_from_tuple_by_type<T, N + 1, Args...>::value;
};

template<class T, class... Args>
T& get_tuple_element_by_type(tuple<Args...>& t)
{
    return get<get_index_of_ele

相关推荐

  1. C++(14):获取类型tuple索引

    2023-12-11 09:20:02       33 阅读
  2. C++(11):判断tuple是否含有某个类型

    2023-12-11 09:20:02       45 阅读
  3. C++(11):forward_as_tuple通过右值构建tuple

    2023-12-11 09:20:02       39 阅读
  4. 使用Elasticsearch同一索引区分不同类型文档

    2023-12-11 09:20:02       14 阅读
  5. 19 # 高级类型索引类型

    2023-12-11 09:20:02       14 阅读
  6. C#使用OpenCV获取图像轮廓

    2023-12-11 09:20:02       42 阅读
  7. ros获取话题发布节点名称(C++)

    2023-12-11 09:20:02       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-11 09:20:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-11 09:20:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-11 09:20:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-11 09:20:02       20 阅读

热门阅读

  1. TCP 和UDP 到底有啥区别

    2023-12-11 09:20:02       37 阅读
  2. 【数据结构】Treap

    2023-12-11 09:20:02       45 阅读
  3. https 加密协议

    2023-12-11 09:20:02       37 阅读
  4. TensorFlow 的基本概念和使用场景

    2023-12-11 09:20:02       41 阅读
  5. OkHttp: 使用入门

    2023-12-11 09:20:02       30 阅读
  6. PCIe中断总结-各个中断的区别

    2023-12-11 09:20:02       49 阅读
  7. 什么是漏洞扫描

    2023-12-11 09:20:02       36 阅读
  8. kubebuilder开发operator

    2023-12-11 09:20:02       26 阅读