C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Extension to std::tuples to allow runtime indexing.

From: Bjorn Reese <breese_at_[hidden]>
Date: Sat, 18 Apr 2026 15:52:18 +0200
Regarding run-time indexing of tuples.

The proposal to add a tuple specialization with a tag type (or a
separate tuple type) does not fit with the current tuple design.

The standard defines a tuple-like concept [tuple.like], and tuple-like
functionality is defined by free functions, such as std::get or
std::tuple_cat.

Therefore, rather than defining a subscript operator, it is better to
define a tuple subscript function that is constrained on tuple-like
types. Something like this:

   template <tuple-like Tuple>
   constexpr auto tuple_at(Tuple&&, size_t index)

This works without any changes to std::tuple, or any specialization
thereof, and it works with all tuple-like types.

A tuple-like visitor should also be considered as an alternative for
a subscripting operator/function. Something like this:

   template <typename Visitor, tuple-like Tuple>
   constexpr auto tuple_visit(Visitor&&, Tuple&&, size_t);

   // Usage
   tuple_visit([] (auto&& arg) { /* do stuff */ },
               tuple, index);

A visitor solution follows existing practices of std::visit(), and it
does not require variant with reference types.

I am going to address variant with reference types in a separate reply.

Received on 2026-04-18 13:52:21