这不是秘密,std::get<i>(tuple)
让很多程序员很烦恼。通过索引C++访问元组元素11
取而代之,我想用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
?
海事组织很少有这么多的工作。我也认为这个例子有一个错误。尝试'0x1_index'或甚至'1.0_index'。 – Simple
@简单。谢谢。 –
我也会说这不值得。 'std :: get (tuple)'这个令人讨厌的事情是它不能与变量一起使用,而不能通过索引操作符来完成。您的实现仍然不提供通过简单变量访问元组。 –