Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost][algorithm] Overload of boost::algorithm::all_of_equal
From: Marshall Clow (mclow.lists_at_[hidden])
Date: 2015-05-21 10:15:12


On Thu, May 21, 2015 at 2:21 AM, <dariomt_at_[hidden]> wrote:

> Hi list,
>
> I usually find useful an overload of this function to check whether all
> elements in a range are equal *to each other*.
>
> Something like:
>
> template <typename Range>
> bool all_of_equal(const Range & r)
> {
> // return true on empty range
> if (boost::empty(r)) return true;
> // return true if all other elements are equal to the first element
> return boost::all_of_equal( boost::next( boost::begin(r) ),
> boost::end(r), *boost::begin(r));
> }
>
> Would this be a useful addition to Boost.Algorithm?
>
>
I'm not enthusiastic about this. Then again, I am not enthusiastic about
all_of, etc in general. I think they're barely convenience functions, they
throw away information (returning a boolean, rather than the position where
the comparison failed), and they can be easily implemented using std::find.

As to your particular proposal, what would you do for a predicate version?
(i.e, not using operator== to compare the elements). The signature
"all_of_equal(const Range &r, const T &val)" is already taken, so we can't
declare "all_of_equal(const Range &r, Predicate p)".

I think that this is a good example of something that you should keep in
your codebase, rather than in Boost.

If you disagree, please let me know.

-- Marshall



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net