对于std :: tuple,如何按类型获取数据,以及如何按索引获取类型? [英] For std::tuple, how to get data by type, and how to get type by index?

查看:74
本文介绍了对于std :: tuple,如何按类型获取数据,以及如何按索引获取类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说明了这一点:给定一个std :: tuple,我想

The title says it: Given an std::tuple, I'd like to


  1. 获取给定类型的第一个元素

  2. 获取第i个元素的类型

是否有解决方案由STL提供?还是解决方法?有人可以尝试完成我的代码吗?

Is there a solution provided by STL? Or a workaround? Can someone try to complete my code?

#include <tuple>

int main ()
{
    std::tuple<int,char,int> mytuple (10,'a', 5);

    // how to get the first int element here? (10)
    // int x = std::get_me_the_first<int>(mytuple);

    // how to get the type of the second element here?
    // std::get_me_type_of<1> ch = 'x';

    return 0;
}

像这样编译:

g++ -std=c++11 -Wall main.cpp -o main


推荐答案

按类型(而不是索引)从元组获取值



从C ++ 11开始,没有STL方法获取类型为 T 的元组的第一个元素。

在C ++ 14中,应该有一种使用 std :: get 的新重载来执行所需的操作。 ISO文件位于此处 N3404 而这里 N3670

Getting value from tuple by type (instead of index)

As of C++11, there is no STL way to get the first element of a tuple of type T.
In C++14, there should be a way using a new overload of std::get to do what you want. The ISO paper is located here N3404 and here N3670.

您可以在C ++ 11中执行以下操作:

You can do this in C++11 with the following:

#include<tuple>
#include<type_traits>
#include<string>
#include<iostream>

template<int Index, class Search, class First, class... Types>
struct get_internal
{
    typedef typename get_internal<Index + 1, Search, Types...>::type type;
        static constexpr int index = Index;
};

template<int Index, class Search, class... Types>
struct get_internal<Index, Search, Search, Types...>
{
    typedef get_internal type;
    static constexpr int index = Index;
};

template<class T, class... Types>
T get(std::tuple<Types...> tuple)
{
    return std::get<get_internal<0,T,Types...>::type::index>(tuple);
}

我将其托管在Ideone上此处,但这是我的后代测试功能

I have it hosted on Ideone here, but here's my test function for posterity

int main()
{
    std::tuple<int, double, std::string> test{1, 1.7, "test"};
    std::cout<<"get<0> == get<int> :"<< (std::get<0>(test) == get<int>(test))<< "\n";
    std::cout<<"get<1> == get<double> :"<<(std::get<1>(test) == get<double>(test))<< "\n";
    std::cout<<"get<2> == get<std::string> :"<<(std::get<2>(test) == get<std::string>(test))<< "\n";
}

基于@Yakk将其扩展为支持多个类型的实例的想法以及要在元组中进行测试的谓词,他提供了以下代码(也托管在Ideone的此处
>
警告:在C ++ 14中,新的 std :: get 重载不允许多个相同类型的实例在元组中。相反,它将发出编译错误。另外,C ++ 14版本也不支持谓词。

Based of @Yakk's idea of extending this to support multiple instances of a type as well as a predicate to test for in the tuple, he provided the code below (also hosted on Ideone here)
Be warned: in C++14 the new overload of std::get does not allow multiple instances of the same type in the tuple. It instead issues a compile error. In addition the C++14 version will not support predicates either.

//Include same headers as before
template<bool b, typename T=void>
using EnableIf = typename std::enable_if<b,T>::type;

template<int Index, template<typename T>class Search, int Which, typename, class First, class... Types>
struct get_internal:
    get_internal<Index + 1, Search, Which, void, Types...>
{};

template<int Index, template<typename T>class Search, int Which, class First, class... Types>
struct get_internal<Index, Search, Which, EnableIf<!Search<First>::value>, First, Types...>:
    get_internal<Index + 1, Search, Which, void, Types...>
{};
template<int Index, template<typename T>class Search, int Which, class First, class... Types>
struct get_internal<Index, Search, Which, EnableIf<Search<First>::value>, First, Types...>:
    get_internal<Index + 1, Search, Which-1, void, Types...>
{};
template<int Index, template<typename T>class Search, class First, class... Types>
struct get_internal<Index, Search, 0, EnableIf<Search<First>::value>, First, Types...>:
    std::integral_constant<int, Index>
{};

template<template<typename>class Test, int Which=0, class... Types>
auto get(std::tuple<Types...>& tuple)->
  decltype(std::get<get_internal<0,Test,Which,void,Types...>::value>(tuple))
{
    return std::get<get_internal<0,Test,Which,void,Types...>::value>(tuple);
}
template<template<typename>class Test, int Which=0, class... Types>
auto get(std::tuple<Types...> const& tuple)->
  decltype(std::get<get_internal<0,Test,Which,void,Types...>::value>(tuple))
{
    return std::get<get_internal<0,Test,Which,void,Types...>::value>(tuple);
}
template<template<typename>class Test, int Which=0, class... Types>
auto get(std::tuple<Types...>&& tuple)->
  decltype(std::move(std::get<get_internal<0,Test,Which,void,Types...>::value>(tuple)))
{
    return std::move(std::get<get_internal<0,Test,Which,void,Types...>::value>(tuple));
}

template<typename T>
struct is_type {
  template<typename U>
  using test = std::is_same<T,U>;
};

template<class T, int Which=0, class... Types>
T& get(std::tuple<Types...>& tuple)
{
    return get<is_type<T>::template test,Which>(tuple);
}
template<class T, int Which=0, class... Types>
T const& get(std::tuple<Types...> const& tuple)
{
    return get<is_type<T>::template test,Which>(tuple);
}
template<class T, int Which=0, class... Types>
T&& get(std::tuple<Types...>&& tuple)
{
    return std::move(get<is_type<T>::template test,Which>(tuple));
}



获取元组中第n个元素的类型



有一种获取第n个元素的类型的方法。 std :: tuple_element< n,decltype(tuple)> :: type (感谢@syam)是元组的第n个元素的类型。

Getting type of n-th element in tuple

There is a way to get the type of the n-th element. std::tuple_element<n, decltype(tuple)>::type (thanks @syam) is the type of the n-th element of the tuple.

这篇关于对于std :: tuple,如何按类型获取数据,以及如何按索引获取类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆