C++ Logo

std-proposals

Advanced search

Re: count-based for loop

From: Matthew Woehlke <mwoehlke.floss_at_[hidden]>
Date: Thu, 29 Aug 2019 09:51:48 -0400
On 28/08/2019 08.23, Andrew Tomazos via Std-Proposals wrote:
> See:
>
> https://docs.google.com/document/d/1gBdBualdIU1bpgW_El4GT-p0he9Yr7D52xmLmHes5Qo/edit?usp=sharing

TBH, I like the name "indices" much better than "upto". It seems a shame
to lose that to a special variant which always casts, which is not
always the right thing. Especially as in many cases you could as easily
write:

  for (auto const i : std::indices(static_cast<size_t>(...)))

...and have the same effect with less change for user confusion. (Also,
size_t can easily be replaced with ssize_t in the above.)

I wonder if instead we could make this work?

  int x = ...;
  for (auto const i : std::indices(x)) // i → int
    ;
  for (auto const i : std::indices<size_t>(x)) // i → size_t
    ;

I'm not sure, though, how to make deduction work so that, with no
arguments, you get back the same type as the input, but *with*
arguments, you accept anything and static_cast to the template type.

-- 
Matthew

Received on 2019-08-29 08:53:54