access tuple elements by index c++11

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-08 05:08:05

问题


It's not secret, std::get<i>(tuple) annoys many programmers.

Instead of it, I want use something like tuple[i].

So I tried to simulate it.

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

template < int > struct index{};



template< char ... >
struct combine;

template<> struct combine<> : std::integral_constant< int , 0>{};


constexpr int ten(size_t p)noexcept
{
    return p == 0 ? 1 : 10 * ten(p-1);
}

template< char c, char ... t>
struct combine<c, t...> : std::integral_constant< int, (c - '0')*ten(sizeof...(t)) + combine<t...>::value > 
{ static_assert(c >= '0' && c <= '9', "only 0..9 digits are allowed");  };


template< char ... c >
constexpr auto  operator "" _index()noexcept 
{ 
    return index< combine<c...>::value >{}; 
}; 

template< class ... Args >
struct mytuple : public std::tuple<Args...>
{
    using std::tuple<Args...>::tuple;

    template< int i >
    auto& operator []( index<i> ) noexcept
    {
        return std::get< i > ( static_cast< std::tuple<Args...> & >(*this) );
    }

    template< int i>
    auto const& operator [](index<i> )const noexcept
    {
        return std::get< i >(static_cast< std::tuple<Args...> const& >(*this) );
    }

};


int main()
{
     static_assert( combine<'1','2','3','4'>::value == 1234, "!");


     static_assert( std::is_same< index<785>, decltype( 785_index ) > {}, "!");

     using person = mytuple< std::string, int, double, char>;

     person s = std::make_tuple("Bjarne Stroustrup", 63, 3.14, '7' );
     auto name = s[ 0_index ];
     auto old  = s[ 1_index ];
     auto number = s[ 2_index ];
     auto symbol = s[ 3_index ];

     std::cout << "name: "   << name << '\t'
               << "old: "    << old << '\t'
               << "number: "  << number<< '\t'
               << "symbol: " << symbol<< '\t'
               << std::endl;
}

Q: What's wrong this code? i.e this code is usable or not? If is usable why isn't std::tuple implemented like this?


回答1:


I'm not really sure what the specific question in your case is, but it seems you are looking for a little bit of convenience. The following uses the placeholders (starting with _1) to simplify your code:

#include <iostream>
#include <type_traits>
#include <tuple>
#include <functional>

template< class ... Args >
struct mytuple : public std::tuple<Args...>
{
    using std::tuple<Args...>::tuple;

    template< typename T >
    auto& operator []( T ) noexcept
    {
        return std::get< std::is_placeholder<T>::value - 1 >( *this );
    }

    template< typename T >
    auto const& operator []( T ) const noexcept
    {
        return std::get< std::is_placeholder<T>::value - 1 >( *this );
    }
};

int main()
{
    using person = mytuple< std::string, int, double, char>;

    using namespace std::placeholders;

    person s = std::make_tuple("Bjarne Stroustrup", 63, 3.14, '7' );
    auto name = s[ _1 ];
    auto old  = s[ _2 ];
    auto number = s[ _3 ];
    auto symbol = s[ _4 ];

    std::cout << "name: "   << name << '\t'
              << "old: "    << old << '\t'
              << "number: "  << number<< '\t'
              << "symbol: " << symbol<< '\t'
              << std::endl;
}

The trick here is to know that std::is_placeholder<T> is guaranteed to be derived from std::integral_constant<int,N> :) Hope you like it.



来源:https://stackoverflow.com/questions/19293382/access-tuple-elements-by-index-c11

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!