C++ Logo

std-proposals

Advanced search

Re: int arr[] = {1..10}; (or for containers)

From: Bo Persson <bo_at_[hidden]>
Date: Sun, 8 Aug 2021 21:45:35 +0200
On 2021-08-08 at 20:44, Jason McKesson via Std-Proposals wrote:
> On Sun, Aug 8, 2021 at 1:10 PM sreeraj c via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
>>
>> Instead of using std::iota or loops, when we have compile time
>> constant values it would be easy to write like this.
>>
>>
>> Thanks
>> Sreeraj
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
> With constexpr ranges, and P1858, we can achieve the same effect
> without further language changes:
>
> int arr[] = {[:]std::ranges::iota(1, 10)...};
>
> There's no need to make such a narrow language change that only
> benefits one small use case when we can just permit something like
> this to work.
>
> That being said, I'm not sure how useful this is in general. Do people
> really need this so often that we need dedicated syntax for it?
>

Yes, you might wonder why it is needed to store the values in a array,
if we know that the values are in a sequence.

So instead of arr[i], you could do f(i) with a simple

constexpr int f(int i) { return i + 1; }

(or wherever the iota sequence starts).

Received on 2021-08-08 14:45:48