c++ - 通过索引访问元组元素 c++11

标签 c++ c++11 stdtuple

这不是 secret ,std::get<i>(tuple)惹恼了许多程序员。

我想使用类似 tuple[i] 的东西来代替它.

所以我尝试模拟它。

#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;
}

问:这段代码有什么问题?即这个代码是否可用? 如果可用为什么不可用 std::tuple像这样实现?

最佳答案

我不太确定您的情况的具体问题是什么,但您似乎正在寻找一点方便。下面使用占位符(以 _1 开头)来简化您的代码:

#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;
}

这里的技巧是要知道 std::is_placeholder<T> 保证源自std::integral_constant<int,N> :) 希望你喜欢它。

关于c++ - 通过索引访问元组元素 c++11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19293382/

相关文章:

C++ 对析构函数的 undefined reference

c++ - 查找素数和时出现内存错误

C++从函数调用的多个返回构建字符串 vector 的最佳方法

c++ - 编译器生成的 Action 的实现

c++ - 如果索引超出范围,如何从 std::tuple_element 返回 void?

c++ - 初始化不可复制和不可移动类的元组

c++ - 在 C++ 中的 double 组中搜索最近的值?

c++ - 如何通过反汇编从 C++ 函数中获取 "lea"指令?

c++ - 模板参数包 : How to create a Tuple of an independent type with same length

c++ - cpp/c++ 获取指针值或去指针化指针