C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Deprecate and eventually remove std::initializer_list

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Fri, 27 Jun 2025 02:10:24 +0100
On Thu, 26 Jun 2025, 23:39 Jason McKesson via Std-Proposals, <
std-proposals_at_[hidden]> wrote:

> So, you want:
>
> ```
> std::array<int, 5> arr{1, 2, 3, 4, 5};
> ```
>
> To mean one thing, while:
>
> ```
> std::vector<int> arr{1, 2, 3, 4, 5};
> ```
>
> To just... not work at all.
>
> Half the point of list initialization syntax is so that containers and
> aggregates can use the same syntax to achieve conceptually identical
> results.
>

I think "conceptually identical" is a bit of a stretch.

It happens to look that way for your example above, because you have an
aggregate with 5 members *of the same type* and a container that you
populate with 5 elements. But one has a fixed size and one does not.

And take a slightly different aggregate and there's no way to have
conceptually identical initialization of a container:

struct S { string str; int x; };
S s{"", 1};

How (and why!) would we initialize a container with {"", 1} ?


I wish we didn't have the same syntax for aggregate init and "here's an
arbitrary sized list of values of the same type, copy them into some
storage".


> Now yes, if the array was of some move-only types, and the
> initializers were all prvalues, this wouldn't work with the `vector`
> version. But it would in basically every other case. What you're
> wanting makes it just never work at all.
>
>

Received on 2025-06-27 01:10:42