Boost logo

Boost :

Subject: [boost] [range] Proposal: addition of front(), back(), at(), operator[]
From: Adam Wulkiewicz (adam.wulkiewicz_at_[hidden])
Date: 2014-03-23 15:59:24


Hi,

I propose to add the functions described below to the Boost.Range. I
found the use of those very convenient WRT the corresponding use of
begin() and end(), and a lot more readable.

1. front()
returning the reference to the first element
precondition (assert): !empty(rng)

return *begin(rng);

2. back()
returning the reference to the last element
precondition (assert): !empty(rng)

For BidirectionalRange
return *(--end(rng));

For SinglePassRange it could iterate over the range and return the
reference to the last element

3. at()
returning the reference to the i-th element of the range
precondition (throw std::out_of_range or assert): index < size(rng)

For RandomAccessRange
return *(begin(rng) + index);

For SinglePassRange it could iterate over the range and return the
required element

For me, non-throwing at() would be more useful however this behavior
won't be intuitive. So the following should also be provided:

4. sub(), subscript(), el(), element(), at_index(), at_(), index(),
indexed(), access(), operator[], ...?
the implementation of indexing operator[]
same as 3 but instead of throwing an assert would be used

The use of operator[] could be similar to this:

{
     using boost::range::subscript_operator;
     rng[index];
}

So not very convenient. But maybe you have a better idea?

Regards,
Adam


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk