C++ Logo


Advanced search

Re: [std-proposals] Missing overloaded constructors for std::initializer_list in some container adapters

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sat, 11 Mar 2023 11:44:59 -0500
On Sat, Mar 11, 2023 at 11:34 AM Giuseppe D'Angelo via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> Hi,
> Il 11/03/23 13:34, LoS via Std-Proposals ha scritto:
> > - since some container adapters also use overloaded constructors for
> > std::initializer_list, there is not reason not to introduce them into
> > the other container adapters.
> Unfortunately there *is* a reason: in the general case you can't add an
> initializer_list constructor "after the fact" and keep source
> compatibility. Consider for instance
> > Foo *ptr;
> > Container c{ptr, ptr+10};
> What does this code do? Does it build a Container<Foo> of 10 elements
> from the [ptr, ptr+10) range, or a Container<Foo *> containing 2
> elements (ptr and ptr+10)?
> For std::queue in C++23 it means the former; which already means we will
> never be able to add an initializer_list constructor, because it would
> change the meaning of the code (to mean the latter). Probably even
> adding the constructor in C++17 would've been too late, because it would
> change the meaning of code like
> > std::deque<int> d;
> > std::queue q{d}; // queue<int> using `d` as inner container, or queue<deque<int>> of 1 element?
> It's a real cone of shame because this *keeps happening*: with
> queue/stack here, with std::span in C++20 (P2447 was rejected for this
> very reason) and who knows if it'll happen again in the future.
> My 2 c,

It's almost like it was a mistake to make "construct a type from a
sequence of values" and "construct a type by calling a constructor
with these values as parameters" use the exact same syntax.

Received on 2023-03-11 16:46:11