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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With