C++ Logo

std-proposals

Advanced search

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

From: Simon Schröder <dr.simon.schroeder_at_[hidden]>
Date: Fri, 27 Jun 2025 06:58:00 +0200
If you want list initialization and aggregate initialization to look differently you need to go back to C first. Currently, we ca write

struct S
{
   int a;
   Int b;
};
S s = { 1, 2 };
int a[] = { 1, 2 };
Do you want to change the syntax for one of these two? Then it needs to be also changes in the C standard. (And you need a really good reason to be incompatible with all existing code that uses both arrays and structs.)

BTW, initializer-list is a term that already existed in the very first C standard. C++11 just made it available as a library type.

On Jun 27, 2025, at 12:56 AM, Maciej Cencora via Std-Proposals <std-proposals_at_[hidden]> wrote:


They already mean different things, the first one is aggregate initialization with brace-ellision, the other one is a list-initialization that invokes a user-defined constructor taking an std::initializer_list. And that already doesn't mean the same thing - for aggregate if elements are initialized from prvalue there is no-copy nor move, while for classes the copy is unavoidable. 

The problem is that these mean different things:
std::vector<int> a(1, 2);
std::vector<int> a{1, 2};

For me they should only differ in one thing - list initialization should fail if it would trigger narrowing conversion (as it does now), but otherwise it should select the same constructor as direct-initialization (proposed behaviour).

Regards,
Maciej

pt., 27 cze 2025 o 00:38 Jason McKesson via Std-Proposals <std-proposals_at_[hidden]> napisał(a):
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.

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.
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-06-27 04:58:13