WARNING: This is a draft.
Currently in order to access tuples, we need to use the
std::get<>
family of functions. This is non-uniform to
the usual way we index the rest of the types and also
more verbose.
The user expectation is usually that a tuple should be indexable
with operator[]. This convention would give more interface uniformity:
vector
, array
, map
, unordered_map
and plain arrays.
I propose to add operator[]
to std::tuple
making use of
std::integral_constant
.
This way we could do the following:
template <std::size_t I>
constexpr std::integral_constant<std::size_t, I> c;
auto fib = make_tuple(1, 1, 2, 3, 5, 8, 13, 21);
//Before:
f(std::get<0>(fib), std::get<1>(fib), std::get<2>(fib), std::get<3>(fib),
std::get<4>(fib));
//Now:
f(fib[c<0>], fib[c<1>], fib[c<2>], fib[c<3>], fib[c<4>]);
//Or with a standard user-defined literal:
f(fib[0is], fib[1is], fib[2is], fib[3is], fib[4is]);
Note: having user-defined literals or var templates
for std::integral_constant
would be part of another proposal.
Indexing by type could also be achieved via a type wrapper.
template <class T>
constexpr type_<T> type;
auto person = make_tuple("John"s, 24);
//Before
auto const & name = std::get<std::string>(person);
//Now
auto const & name = person[type<std::string>];
Note: having type wrappers would be part of another proposal.
TODO: Mention Boost.Hana tuples.
- Should we have standard variable templates or user-defined literals
for
std::integral_constant
? - Should
std::get<>
get deprecated? - Should we add
operator[]
tostd::pair
?
//Index access
template <class...Types>
template<std::size_t I>
constexpr std::tuple_element_t<I, tuple<Types...> >&
std::tuple<Types...>::operator[](std::integral_constant<std::size_t, I>) &;
template <class...Types>
template<std::size_t I>
constexpr std::tuple_element_t<I, tuple<Types...> >&&
std::tuple<Types...>::operator[](std::integral_constant<std::size_t, I>) &&;
template <class...Types>
template<std::size_t I>
constexpr std::tuple_element_t<I, tuple<Types...> >&&
std::tuple<Types...>::operator[](std::integral_constant<std::size_t, I>) const;
//Type access
template <class...Types>
template<class T>
constexpr T &
std::tuple<Types...>::operator[](type<T>) &;
template <class...Types>
template<class T>
constexpr T &&
std::tuple<Types...>::operator[](type<T>) &&;
template <class...Types>
template<class T>
constexpr T const &
std::tuple<Types...>::operator[](type<T>) const;
- List here Boost.Hana author and relevant links.