C++ Logo

STD-PROPOSALS

Advanced search

Subject: Re: [std-proposals] count-based for loop
From: Tony V E (tvaneerd_at_[hidden])
Date: 2019-09-01 14:30:25


upto -> indexto

We know indexing is zero based.‎ Whereas "up to 10 people in this elevator" includes 10.


Sent from my BlackBerry portable Babbage Device
From: John McFarlane via Std-Proposals
Sent: Thursday, August 29, 2019 3:02 AM
To: std-proposals@lists.isocpp.org
Reply To: std-proposals@lists.isocpp.org
Cc: John McFarlane
Subject: Re: [std-proposals] count-based for loop

On Wed, 28 Aug 2019 at 13:23, Andrew Tomazos via Std-Proposals <std-proposals@lists.isocpp.org> wrote:
See:

https://docs.google.com/document/d/1gBdBualdIU1bpgW_El4GT-p0he9Yr7D52xmLmHes5Qo/edit?usp=sharing

Thanks for writing this. I think it takes us in the right direction.

I wonder whether the name `upto` should change: `upto(10)` doesn't count 'up to' 10 in the sense most users will expect. If it remains, it will mark the lesson in which students gain hard-earned knowledge about half-closed ranges.

`indices` should use `ssize`, and not `size`. We should be pushing to encourage signed integers in this situation as `i` is a quantity. This reflects advice in ES.102 of the C++ Core Guidelines.

John



On Wed, Aug 28, 2019 at 3:42 AM Sophia Poirier via Std-Proposals <std-proposals@lists.isocpp.org> wrote:
On Aug 27, 2019, at 10:26 AM, Barry Revzin <barry.revzin@gmail.com> wrote:

On Tue, Aug 27, 2019, 12:02 PM Sophia Poirier via Std-Proposals <std-proposals@lists.isocpp.org> wrote:
Thanks, yes I have that as an alternate example in my longer notes.  My understanding is that it still suffers from the type-matching problem as traditional for loops.  Your example of:

for (const auto i : std::views::iota(0, count))

only works when count is an int.  Otherwise, if for example count is uint32_t, it would need to be:

for (const auto i : std::views::iota(uint32_t{0}, count)
or:
for (const auto i : std::views::iota(0u, count))
or:
for (const auto i : std::views::iota<uint32_t>(0, count))
or:
for (const auto i : std::views::iota<decltype(count)>(0, count))

or something along those lines, or you will get template instantiation failure compiler error.  I think that if std::views::iota had a constructor overload that was simply the second argument (count) with implicit zero start, then it would be a good option.  However I believe there is interest to reserve such an overload perhaps for infinite ranges?

thanks,
Sophia

This is true. But we can write a helper function to get the correct type of 0 so we don't need the ugliness at point of use:

template <std::integral T>
auto upto(T n) {
    return views::iota(T{0}, n);
}

We end up with:

for (const auto i : upto(count))

Barry

This could be the basis of an alternate library proposal, true.

- Sophia
--
Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
--
Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals


STD-PROPOSALS list run by herb.sutter at gmail.com

Standard Proposals Archives on Google Groups