C++ Logo

STD-PROPOSALS

Advanced search

Subject: Re: [std-proposals] [External] std::to_array support for zero-length arrays
From: Arthur O'Dwyer (arthur.j.odwyer_at_[hidden])
Date: 2020-11-04 15:32:04


On Wed, Nov 4, 2020 at 3:58 PM D'Alessandro, Luke K <ldalessa_at_[hidden]> wrote:

> On Nov 4, 2020, at 11:54 AM, Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
> wrote:
> On Wed, Nov 4, 2020 at 2:22 PM D'Alessandro, Luke K <ldalessa_at_[hidden]>
> wrote:
>
>> Hi Arthur,
>>
>> On Nov 4, 2020, at 10:46 AM, Arthur O'Dwyer via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>> If you're trying to use std::to_array with explicit template arguments,
>> as in
>> auto f3 = std::to_array<float>({1, 2, 3});
>> auto f0 = std::to_array<float>({});
>> then I think that usage needs justification (by which I actually mean, *that
>> usage is wrong*).
>>
>> This is the use case that occurs with some frequency in real code, and is
>> explicitly referenced numerous times in
>> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0325r3.html. I
>> don't feel like it is wrong.
>>
>
> I do. :)
> I see it mentioned once in p0325r4's examples (but never called out as a
> primary use-case), and zero times in the actual paper standard.
>
>
> We must be looking at different versions of r4 then (
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0325r4.html).
> It's prominent in the introduction and appears a number of times in the
> impact on the standard.
>

Hm, yes, it's mentioned several times. Okay.

Anyway, I would be mildly surprised if your code couldn't also benefit from
> the advice in
> https://quuxplusone.github.io/blog/2020/08/06/array-size/ ;)
>
> I'm not sure if this is snark or self promotion or an actual attempt to be
> helpful. Given the emoji I assume a bit of the first two, not much of the
> third.
>

Well, the post's advice is orthogonal to your original proposal about
std::to_array<T>({}) -- but, IMO, that original problem is completely solved
by saying "just use std::array<T,0>{} or
std::array<T,sizeof...(args)>{args...} as appropriate, which has the
benefit of speeding up your code." My advice to *for additional speedup,
additionally stop using std::array* may not be immediately actionable for
you.
If you're *able* to follow the advice in the post (such as "avoid
std::array" and "avoid manually counting array elements"), it might be very
helpful. But I suspect that you might be operating in a domain, or on an
arcanely tangled codebase, where advice of the form "Just don't do *X*"
doesn't count as helpful.

> [from blog]
>
> Anyway, all of these options result in a lot of extra template
> instantiations, compared to plain old C-style arrays (which require zero
> template instantiations). Therefore I strongly
> prefer T[] over std::array<T, N>.
>
> Indeed. I would much prefer to use a C array (+span as necessary) in my
> generic constexpr code, but the the context happens to sometimes be
> zero-length. I have considered (am strongly considering) enabling
> zero-length-array extensions as the alternative to std::array.
>

-Arthur



STD-PROPOSALS list run by std-proposals-owner@lists.isocpp.org

Standard Proposals Archives on Google Groups