Date: Sat, 13 Feb 2021 07:49:43 +0000
Hey, I just had some code that I wanted to write where I list initialize
a struct from a tuple.
Of course C++ has std::make_from_tuple, but this uses direct
initialization. That is
struct Foo {
int a;
int b;
};
cannot be initialized using std::make_from_tuple<Foo, std::tuple<int, int>>.
I can copy and paste the "Possible Implementation" from
https://en.cppreference.com/w/cpp/utility/make_from_tuple and change T()
to T{} and it works just fine.
However, I was wondering if there are any references as to why that
function uses direct initialization over list initialization, or what
your initial thoughts are to making it possible to list-initialize from
a tuple as a standard library function.
I'm not really up-to-date on why list initialization and direct
initialization diverged / exist as two distinct kinds of initialization
in the first place, so maybe there are very good reasons why direct
initialization was chosen for std::make_from_tuple.
Thanks,
Jefferson
P.S. Structs and tuples are both Cartesian product types - that is they
both implement a logical AND between types - although structs are
strictly more powerful in that they can have private members and their
behavior can be given algorithmically and not just algebraically.
However, for my private use case I'm also considering using std::tuples
exclusively, and parameterizing std::get over an enum class to access
members. The advantage of std::tuple is that you can write templated
code that operates on the parameter pack to write certain meta-programs,
whereas you can't currently inspect the members of structs.
a struct from a tuple.
Of course C++ has std::make_from_tuple, but this uses direct
initialization. That is
struct Foo {
int a;
int b;
};
cannot be initialized using std::make_from_tuple<Foo, std::tuple<int, int>>.
I can copy and paste the "Possible Implementation" from
https://en.cppreference.com/w/cpp/utility/make_from_tuple and change T()
to T{} and it works just fine.
However, I was wondering if there are any references as to why that
function uses direct initialization over list initialization, or what
your initial thoughts are to making it possible to list-initialize from
a tuple as a standard library function.
I'm not really up-to-date on why list initialization and direct
initialization diverged / exist as two distinct kinds of initialization
in the first place, so maybe there are very good reasons why direct
initialization was chosen for std::make_from_tuple.
Thanks,
Jefferson
P.S. Structs and tuples are both Cartesian product types - that is they
both implement a logical AND between types - although structs are
strictly more powerful in that they can have private members and their
behavior can be given algorithmically and not just algebraically.
However, for my private use case I'm also considering using std::tuples
exclusively, and parameterizing std::get over an enum class to access
members. The advantage of std::tuple is that you can write templated
code that operates on the parameter pack to write certain meta-programs,
whereas you can't currently inspect the members of structs.
Received on 2021-02-13 01:49:46