C++ Logo

STD-PROPOSALS

Advanced search

Subject: Re: [std-proposals] function at() for span<> that throws exception on out-of-range, like vector and array ::at()
From: Ville Voutilainen (ville.voutilainen_at_[hidden])
Date: 2019-11-30 11:41:42


On Sat, 30 Nov 2019 at 19:35, Ville Voutilainen
<ville.voutilainen_at_[hidden]> wrote:
>
> On Sat, 30 Nov 2019 at 18:55, Lyberta via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > John McFarlane via Std-Proposals:
> > > Sanitizing user input sounds like something that is best served by explicit
> > > logic. I certainly don't think that UI is any concern of containers.
> >
> > Explicit logic such as
> >
> > if (i >= std::size(whatever))
> > {
> > throw std::out_of_range{"Invalid index."};
> > }
> >
> > How is that better than calling at()?
>
> I can't find either innocent or non-innocent users who would find it
> better.

Note, though, that there is this aspect of it:

rather than write:

do_something(foo.at(bar));
do_something(foo.at(bax));
do_something(foo.at(baz));

you can write

if (bar >= 0 && baz <= foo.size() ) {
  do_domething(foo[bar]);
  do_something(foo[bax]);
  do_something(foo[baz]);
}

So, rather than looking at single-condition cases, we should remember
to consider multi-condition ones.


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

Standard Proposals Archives on Google Groups