What's the most simple tuple implementation in C++26?

141 views Asked by At

C++26 is seeing some progress in making template parameter packs more ergonomic. For example P2662: Pack indexing has been accepted into the standard. With this and other related proposals, what's the most simple tuple implementation one could make?

I'm envisioning something along the lines of:

template <typename... T>
struct tuple {
    T... m;
};

tuple<int, float> tup{};
float f = tup.m[1];

Will it be possible to write something like this? If not exactly with this syntax, then how? If not at all, what proposals are missing to make this happen?

1

There are 1 answers

2
HolyBlackCat On

Here you go:

#include <iostream>
#include <utility>

template <typename ...P>
auto make_my_tuple(P &&... args)
{
    return [...args = std::forward<P>(args)]<std::size_t I> -> auto &&
    {
        return args...[I];
    };
}

int main()
{
    auto t = make_my_tuple(1, 2.2f, 3.3);
    std::cout << t.operator()<1>() << '\n'; // Like `get<1>()`.
}

I'm half-joking, obviously. But you could create a template class that wraps such lambdas and gives them a decent interface.