|
Boost : |
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2008-07-04 12:09:57
Finding algorithms (find, find_if, max_element, and others) usually
return an iterator to the found position, or the past-the-end iterator.
While enough, it can be quite annoying when used with range adaptors and
the like.
Imagine something like this:
auto r = some_range | some adapters...;
auto it = max_element(r, comparison_function);
if(it != end(r))
{
do_something;
}
Without auto, it is simply unusable.
If such algorithms returned the range [found, end), we would be able to
write:
if(!empty(max_element(some_range | some adapters..., comparison_function))
{
do_something;
}
Particularly, I find it practical to use it with a function like that:
optional<typename range_value<Range>::type>
deref_opt(const Range& r)
{
typedef typename range_value<Range>::type T;
return empty(r) ? optional<T>() : optional<T>(*begin(r));
}
or like that:
typename range_value<Range>::type
deref_def(const Range& r)
{
typedef typename range_value<Range>::type T;
return empty(r) ? T() : *begin(r);
}
For compatibility, maybe returning a range type that implicitly converts
to the beginning iterator could work.
Do people think this is a good idea?
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk