Date: Tue, 07 Jun 2022 17:54:57 +0100
Dear Experts,
Today I have been frustrated by std::tuple not being an aggregate.
I have some non-moveable types and it is difficult to use them
with tuples. That's in contrast to std::pair, which I can use
with piecewise_construct, and with std::array and plain structs,
which I can use thanks to copy elision:
struct NonMoveable
{
NonMoveable(NonMoveable&&) = delete;
NonMoveable(const NonMoveable&) = delete;
NonMoveable(int a, int b);
};
struct S
{
NonMoveable a;
NonMoveable b;
};
void f()
{
std::pair<NonMoveable,NonMoveable> p{ std::piecewise_construct,
std::make_tuple(1,2),
std::make_tuple(3,4) };
std::array<NonMoveable,2> a{{ {1,2}, {3,4} }};
S s{ {1,2}, {3,4} };
std::tuple<NonMoveable,NonMoveable> t .....????.....
}
I ended up with the following, which I call "structuple" because it
is a hybrid struct and tuple:
template <typename... Ts>
struct structuple
{};
template <typename T0>
struct structuple<T0>
{
T0 f0;
};
template <typename T0, typename T1>
struct structuple<T0,T1>
{
T0 f0;
T1 f1;
};
template <typename T0, typename T1, typename T2>
struct structuple<T0,T1,T2>
{
T0 f0;
T1 f1;
T2 f2;
};
So, what can we do to make std::tuple more like a struct?
One obvious "fix" would be to add std::piecewise_construct support
to std::tuple.
But I was wondering if there is another approach that would allow
tuple to be more like a struct, if we added something to the core
language. Pseudo-code:
template <typename... TYPES>
struct tuple {
TYPES fields;...
};
I note that we can use pack expansion with ",". Why not also with ";" ?
Any thoughts?
Phil.
Today I have been frustrated by std::tuple not being an aggregate.
I have some non-moveable types and it is difficult to use them
with tuples. That's in contrast to std::pair, which I can use
with piecewise_construct, and with std::array and plain structs,
which I can use thanks to copy elision:
struct NonMoveable
{
NonMoveable(NonMoveable&&) = delete;
NonMoveable(const NonMoveable&) = delete;
NonMoveable(int a, int b);
};
struct S
{
NonMoveable a;
NonMoveable b;
};
void f()
{
std::pair<NonMoveable,NonMoveable> p{ std::piecewise_construct,
std::make_tuple(1,2),
std::make_tuple(3,4) };
std::array<NonMoveable,2> a{{ {1,2}, {3,4} }};
S s{ {1,2}, {3,4} };
std::tuple<NonMoveable,NonMoveable> t .....????.....
}
I ended up with the following, which I call "structuple" because it
is a hybrid struct and tuple:
template <typename... Ts>
struct structuple
{};
template <typename T0>
struct structuple<T0>
{
T0 f0;
};
template <typename T0, typename T1>
struct structuple<T0,T1>
{
T0 f0;
T1 f1;
};
template <typename T0, typename T1, typename T2>
struct structuple<T0,T1,T2>
{
T0 f0;
T1 f1;
T2 f2;
};
So, what can we do to make std::tuple more like a struct?
One obvious "fix" would be to add std::piecewise_construct support
to std::tuple.
But I was wondering if there is another approach that would allow
tuple to be more like a struct, if we added something to the core
language. Pseudo-code:
template <typename... TYPES>
struct tuple {
TYPES fields;...
};
I note that we can use pack expansion with ",". Why not also with ";" ?
Any thoughts?
Phil.
Received on 2022-06-07 16:54:59