Topic: 25.2.1 [alg.all_of] all_of return value description is wrong
Author: Steve Ward <planet36@gmail.com>
Date: Wed, 10 Nov 2010 09:42:52 CST Raw View
(n3126.pdf)
25.2.1 All of [alg.all_of]
template <class InputIterator, class Predicate>
bool all_of(InputIterator first, InputIterator last, Predicate pred);
1 Returns: true if [first,last) is empty or if pred(*i) is true for
every iterator i in the range [first,last), and false otherwise.
2 Complexity: At most last - first applications of the predicate.
all_of should not return true if the range is empty.
Here is a better description of the return value for all_of.
Returns: true if [first,last) is not empty and if pred(*i) is true
for every iterator i in the range [first,last), and false otherwise.
Additionally, I think the description of the return value for any_of
can be improved.
Returns: true if [first,last) is not empty and if pred(*i) is true
for any iterator i in the range [first,last), and false otherwise.
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: SG <s.gesemann@gmail.com>
Date: Fri, 12 Nov 2010 08:39:26 CST Raw View
On 10 Nov., 16:42, Steve Ward wrote:
> (n3126.pdf)
> 25.2.1 All of [alg.all_of]
> template <class InputIterator, class Predicate>
> bool all_of(InputIterator first, InputIterator last, Predicate pred);
> 1 Returns: true if [first,last) is empty or if pred(*i) is true for
> every iterator i in the range [first,last), and false otherwise.
> 2 Complexity: At most last - first applications of the predicate.
>
> all_of should not return true if the range is empty.
Why?
std::all_of is an n-ary logical conjunction.
std::any_of is an n-ary logical disjunction.
It's just natural to extend it this way
(a && b && c && ...) == (true && a && b && c ...)
(a || b || c || ...) == (false || a || b || c || ...)
That leaves true in the first case and false in the second case if
there are no other operands. In my opinion std::all_of should return
true and std::any_of should return false in case of an empty range.
And this is how it's currently defined.
Cheers!
SG
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Martin Bonner <martinfrompi@yahoo.co.uk>
Date: Fri, 12 Nov 2010 08:39:21 CST Raw View
On Nov 10, 3:42 pm, Steve Ward <plane...@gmail.com> wrote:
> (n3126.pdf)
> 25.2.1 All of [alg.all_of]
> template <class InputIterator, class Predicate>
> bool all_of(InputIterator first, InputIterator last, Predicate pred);
> 1 Returns: true if [first,last) is empty or if pred(*i) is true for
> every iterator i in the range [first,last), and false otherwise.
> 2 Complexity: At most last - first applications of the predicate.
>
> all_of should not return true if the range is empty.
Why not? If the range is empty, the predicate *has* returned true for
every element in the range.
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =?ISO-8859-1?Q?Falk_Tannh=E4user?= <tannhauser86549spam@free.fr>
Date: Fri, 12 Nov 2010 08:50:31 CST Raw View
Am 10.11.2010 16:42, schrieb Steve Ward:
>
> (n3126.pdf)
> 25.2.1 All of [alg.all_of]
> template<class InputIterator, class Predicate>
> bool all_of(InputIterator first, InputIterator last, Predicate pred);
> 1 Returns: true if [first,last) is empty or if pred(*i) is true for
> every iterator i in the range [first,last), and false otherwise.
> 2 Complexity: At most last - first applications of the predicate.
>
> all_of should not return true if the range is empty.
But the predicate *is* trivially true for every iterator in an empty
range - there is simply no iterator for which it could be false! An
implication is true if the antecedent is false. See
<http://en.wikipedia.org/wiki/Vacuous_truth>.
>From a mathematical point of view, the statements
"There is no prime number between 114 and 126",
"The set of prime numbers between 114 and 126 is empty" and
"For all elements x of the empty set, x is a prime number between 114 and 126"
are all alike.
Actually, the part of clause 25.2.1 "... true if [first,last) is
empty..." is redundant as it is already comprised by the rest of the
phrase "... or if pred(*i) is true for every iterator i in the range
[first,last)".
To resume, all_of() for a given range and predicate is equivalent to
none_of() with the same range and the negated predicate.
> Additionally, I think the description of the return value for any_of
> can be improved.
> Returns: true if [first,last) is not empty and if pred(*i) is true
> for any iterator i in the range [first,last), and false otherwise.
Well, once again, the first part of the phrase "...if [first,last) is
not empty..." is redundant, since "...true for any iterator in the
range..." is synonym for "...true for at least one iterator in the
range..." which already implies that the range is not empty.
Furthermore, any_of() is equivalent to the negation of none_of() with
the same range and predicate.
Of course, being redundant doesn't mean these parts should be removed
as long as they add to clarity / understandability.
HTH
Falk
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Steve Ward <planet36@gmail.com>
Date: Mon, 22 Nov 2010 01:04:47 CST Raw View
On Nov 12, 9:39 am, SG <s.gesem...@gmail.com> wrote:
> On 10 Nov., 16:42, Steve Ward wrote:
>
> > (n3126.pdf)
> > 25.2.1 All of [alg.all_of]
> > template <class InputIterator, class Predicate>
> > bool all_of(InputIterator first, InputIterator last, Predicate pred);
> > 1 Returns: true if [first,last) is empty or if pred(*i) is true for
> > every iterator i in the range [first,last), and false otherwise.
> > 2 Complexity: At most last - first applications of the predicate.
>
> > all_of should not return true if the range is empty.
>
> Why?
> std::all_of is an n-ary logical conjunction.
> std::any_of is an n-ary logical disjunction.
> It's just natural to extend it this way
>
> (a && b && c && ...) == (true && a && b && c ...)
> (a || b || c || ...) == (false || a || b || c || ...)
>
> That leaves true in the first case and false in the second case if
> there are no other operands. In my opinion std::all_of should return
> true and std::any_of should return false in case of an empty range.
> And this is how it's currently defined.
>
> Cheers!
> SG
>
> --
> [ comp.std.c++ is moderated. To submit articles, try posting with your ]
> [ newsreader. If that fails, use mailto:std-cpp-sub...@vandevoorde.com ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ:http://www.comeaucomputing.com/csc/faq.html ]
Thanks for the info. I wasn't aware of logical conjunctions and
logical disjunctions. The current definition makes sense in light of
those.
I thought the relationship between none_of, any_of, and all_of was
like the ASCII diagram below.
+---------------+---------------+
| any_of | none_of |
| | |
| +----------+ | |
| | all_of | | |
| | | | |
| +----------+ | |
| | |
+---------------+---------------+
It seems unintuitive to me that all_of returns true for a range which
any_of returns false.
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Miles Bader <miles@gnu.org>
Date: Wed, 24 Nov 2010 17:51:21 CST Raw View
Steve Ward <planet36@gmail.com> writes:
> It seems unintuitive to me that all_of returns true for a range which
> any_of returns false.
It's a pretty standard interpretation though; e.g., see the "every"
[all_of] and "some" [any_of] functions in Common Lisp.
Perhaps more importantly, those particular return values tend to be the
"right thing" in real code, for handling the (usually edge) case where
the input list is empty. Maybe it helps to remember that "all of X" =
"not (any of (not X))".
e.g.:
(1) "if all doors are locked => go to bed (because it's safe)"
(2) "if any door is not locked => worry (because it's not safe)"
In case (1) [all] the "no door" case should return true, but in case
(2) [any], the no door case should return false.
-miles
--
Bride, n. A woman with a fine prospect of happiness behind her.
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]