最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

c++ - How can I make a tuple of types that can be accessed by index without pack indexing? - Stack Overflow

matteradmin8PV0评论

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?

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?

Share Improve this question edited Nov 16, 2024 at 23:17 HeliumHydride asked Nov 16, 2024 at 22:58 HeliumHydrideHeliumHydride 811 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 5

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;

};

If you are interested how it is done inside std::tuple, then it is as simple as this:

template <std::size_t Index, typename ...T>
struct NthType;
//{};

template <typename T0, typename ...T>
struct NthType<0, T0, T...>
{
    using Type = T0;
};

template <std::size_t Index, typename T0, typename ...T>
struct NthType<Index, T0, T...>
{
    using Type = typename NthType<Index-1, T...>::Type; // skip one type, decrease index
};

template <typename... Ts>
struct type_tuple {
    template <unsigned Index>
    using get = typename NthType<Index, Ts...>::Type;
};

So:

  1. Define helper class NthType<Index, T...>
  2. Specialize for Index = 0: NthType<0, T0, T...>::Type == T0
  3. Make linear search for Nth type: NthType<Idx, T0, T...>::Type == NthType<Idx - 1, T...>

Note, that in last point we skip first type from pack, and decrease the index. In many libraries (including std lib implementations) there are also specialization for Index==1 and Index==2 just for spead up. I am not sure that, but in some compilers I can imagine there are builtin functions to get nth type from a pack - similar to C++26 way.

Before C++11 (before introduction of variadic templates) - there was just 50 or so specialization of such NthType - so it works up to Index==50 (or so...)

Post a comment

comment list (0)

  1. No comments so far