c++parameter-packc++26

How can I make a tuple of types that can be accessed by index without pack indexing?


Using C++26's pack indexing, you can create a simple tuple of types like so:

template <typename... Ts>
struct type_tuple {
    template <unsigned Index>
    using get = Ts...[Index];
};

using T = type_tuple<int, float, double>::get<1>; // float

How can this be replicated in earlier C++ versions?


Solution

  • You can use std::tuple_element. This will work in all C++ versions supporting parameter packs:

    template <typename... Ts>
    struct type_tuple {
    
    template <std::size_t Index>
    using get = typename std::tuple_element<Index, std::tuple<Ts...>>::type;
    
    };