C++ Logo

STD-DISCUSSION

Advanced search

Subject: Re: List initialize from tuple
From: Bo Persson (bo_at_[hidden])
Date: 2021-02-13 02:28:20


On 2021-02-13 at 08:49, Jefferson Carpenter via Std-Discussion wrote:
> 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.
>
>

The choice of initializer method makes a difference for types with a
constructor taking an std::initializer_list parameter. That constructor
greedily accepts all possible {}-initializers.

So, for example

vector<int> v(1,2);

is different from

vector<int> v{1,2};

As Ville points out, the solution apparently was to generalize the ()
initializer, not to change make_from_tuple.

     Bo Persson


STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups