[boost][algorithm] Overload of boost::algorithm::all_of_equal
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?
On Thu, May 21, 2015 at 2:21 AM,
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
participants (2)
-
dariomt@gmail.com
-
Marshall Clow