Skip to content

germandiagogomez/tup-op-brackets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Adding operator[] to std::tuple

WARNING: This is a draft.

Rationale

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.

Proposal

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

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.

Implementation experience

TODO: Mention Boost.Hana tuples.

Open questions

  • Should we have standard variable templates or user-defined literals for std::integral_constant?
  • Should std::get<> get deprecated?
  • Should we add operator[] to std::pair?

New signatures

//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;

References

  • List here Boost.Hana author and relevant links.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published