C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Literal ranges

From: Zhihao Yuan <zy_at_[hidden]>
Date: Tue, 11 Mar 2025 19:13:51 +0000
On Tuesday, March 11th, 2025 at 10:41 AM, Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]> wrote:

> On Thu, Mar 6, 2025 at 5:57 PM Zhihao Yuan <zy_at_[hidden]> wrote:
>

> > On Thursday, March 6th, 2025 at 10:43 AM, Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]> wrote:
> >

> > > On Tue, Mar 4, 2025 at 3:03 PM Zhihao Yuan <zy_at_[hidden]> wrote:
> > >

> > > > If someone is designing their own container,
> > > > a simple rvalue reference to array is usually
> > > > enough
> > > >

> > > > template<size_t N>
> > > > vector(T (&&arr)[N]);
> > > >

> > > > // use
> > > > vector v1({2, 4});
> > > > vector v2({std::make_unique<int>(1), std::make_unique<int>(2)});
> > > >

> > > >

> > > > https://godbolt.org/z/4fbYc37n6
> > >

> > >

> > > I would be skeptical of that, though.
> > > (1) That disables the "normal" way of constructing from a bag of elements:
> > > vector v1 = {2, 4}; // no longer works
> >

> >

> > We are used to it, but you already know the pitfalls.
> > vector v1 = {a.begin(), a.end()};
>

>

> Well, that's an anti-pitfall in the case of std::vector:


When you remove the '=' it's no longer that
obvious.


> Remember, curlies mean "sequence of elements of an aggregate, or something pretending to be an aggregate (such as a product type, container, or span)."



I support that practice, but the reality is
that you can grant what curly mean only if
they do not work outside that meaning.
Otherwise, it's a paradox.


>

> The thing is, if you make your non-template VectorOfInt such that
> VectorOfInt v = {1, 2};
> doesn't compile, then it's going to suck. I don't think you can get away with that level of user-hostility in container design.


If you define user-hostile as "not looking good,"
I would define user-hostile as "not always what user wants."
`Vec x({2, 3});` constructs an object of `Vec` with a
braced-init-list `{2, 3}`, does exactly what user intends,
with no other interpretation, and also works for
constructors taking a single `std::initializer_list`.


> > > (2) That disables P2752 static-storage optimization: [...]
>

>

> But that still blows your stack ( https://godbolt.org/z/Yo7nMPsav ) because temporary arrays don't get the two things you just said:—
> - There is no "backing array" (no P2752 treatment — again, I'll take the blame for this, but it's a fact); there's just a huge temporary on the stack.


More work needed, as usual, to make
the outcome better than most of the
compilers on the market as of 2024.


> - IIUC there is no "implying that the data is immutable," because can't the callee still const_cast the pointer? Or are the elements of temporary arrays actually const objects, such that it's UB to modify them? I'm like 70% sure they're not actually const objects.
>



https://eel.is/c++draft/dcl.init.list#3.10 ?

--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
_______________________________________________

Received on 2025-03-11 19:14:03