Topic: relational operators and pointers
Author: "Boris" <boris@gtemail.net>
Date: 2000/06/21 Raw View
The C++ Standard says somewhere:
"For templates greater, less, greater_equal, and less_equal, the
specializations for any pointer type yield a total order, even if the
built-in operators <, >, <=, >= do not."
Does this implicitly mean that == and != are ok for comparing pointers no
matter what pointers are compared?
Somewhere else I found:
"If two pointers p and q of the same type point to the same object or
function [...] then p<=q and p>=q both yield true and p<q and p>q both
yield false."
This is in C++:
A *p1 = new A;
A *p2 = p1;
p1 == p2; // true
p1 != p2; // false
But then the Standard says:
"If two pointers p and q of the same type point to different objects that
are not members of the same object [...] the results of p<q, p>q, p<=q, and
p>=q are unspecified."
In C++:
A *p1 = new A;
A *p2 = new A;
p1 == p2; // unspecified
p1 != p2; // unspecified
But this is stupid as you are allowed only to compare pointers that point to
the same object because the rest is unspecified. That means you have to know
whether pointers point to the same object before you compare them! Do I
misunderstand the Standard (I really hope so)?
Boris
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Hyman Rosen <hymie@prolifics.com>
Date: 2000/06/21 Raw View
"Boris" <boris@gtemail.net> writes:
> Does this implicitly mean that == and != are ok for comparing pointers no
> matter what pointers are compared?
Yes. See <http://www.comeaucomputing.com/iso/cwg_active.html#73> for
a proposed resolution to a defect report.
> "If two pointers p and q of the same type point to different objects that
> are not members of the same object [...] the results of p<q, p>q, p<=q, and
> p>=q are unspecified."
> In C++:
> A *p1 = new A;
> A *p2 = new A;
> p1 == p2; // unspecified
> p1 != p2; // unspecified
How do you derive == and != being unspecified from the quoted statement?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/06/22 Raw View
Boris wrote:
>
> The C++ Standard says somewhere:
> "For templates greater, less, greater_equal, and less_equal, the
> specializations for any pointer type yield a total order, even if the
> built-in operators <, >, <=, >= do not."
> Does this implicitly mean that == and != are ok for comparing pointers no
> matter what pointers are compared?
No. The fact that == and != are OK is unrelated to the fact that
std::less<T*> yields a total order. The method used by std::less<T*> to
order pointers may be quite different from that used by <. The method
used by == to compare pointers for equality may be connected to the
method used by < to order them, but not necessarily to that used by
std::less<T*>.
> Somewhere else I found:
> "If two pointers p and q of the same type point to the same object or
> function [...] then p<=q and p>=q both yield true and p<q and p>q both
> yield false."
> This is in C++:
> A *p1 = new A;
> A *p2 = p1;
> p1 == p2; // true
> p1 != p2; // false
>
> But then the Standard says:
> "If two pointers p and q of the same type point to different objects that
> are not members of the same object [...] the results of p<q, p>q, p<=q, and
> p>=q are unspecified."
> In C++:
> A *p1 = new A;
> A *p2 = new A;
> p1 == p2; // unspecified
> p1 != p2; // unspecified
>
> But this is stupid as you are allowed only to compare pointers that point to
> the same object because the rest is unspecified. That means you have to know
> whether pointers point to the same object before you compare them! Do I
> misunderstand the Standard (I really hope so)?
Yes. You're not making the distinction between relational operators, and
equality operators. p<q, p<=q, p>=q, and p>q are relational operations.
p==q and p!=q are equality operators. Section 5.9 which you've just
quoted is the part that prohibits relational operations between
pointers, unless they point into the same object. Section 5.10,
describing equality operations, contains no such restriction. Instead,
it says "Two pointers of the same type compare equal if and only if they
are both null, both point to the same object or function, or both point
one past the end of the same array."
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Barry Margolin <barmar@genuity.net>
Date: 2000/06/22 Raw View
In article <8iog9l$2bo$1@news.rrz.Uni-Koeln.DE>,
Boris <boris@gtemail.net> wrote:
>The C++ Standard says somewhere:
>"For templates greater, less, greater_equal, and less_equal, the
>specializations for any pointer type yield a total order, even if the
>built-in operators <, >, <=, >= do not."
>Does this implicitly mean that == and != are ok for comparing pointers no
>matter what pointers are compared?
Yes. If you couldn't compare pointers to different objects for equality,
you would be really screwed.
--
Barry Margolin, barmar@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/06/22 Raw View
In article <8iog9l$2bo$1@news.rrz.Uni-Koeln.DE>, Boris
<boris@gtemail.net> writes
>But this is stupid as you are allowed only to compare pointers that point to
>the same object because the rest is unspecified. That means you have to know
>whether pointers point to the same object before you compare them! Do I
>misunderstand the Standard (I really hope so)?
Possibly (the templates you started with are a red-herring). You are
always allowed to compare for equality any two valid pointers (look
elsewhere for a discussion for what makes a pointer valid). That means
that providing p1 and p2 are valid pointers you can always evaluate
p1==p2 and p1!=p2. However you can only apply the other four comparison
operators to pointers that point into the same object. The template
versions may have to walk on eggshells to get round problems with
pointers that do not point to somewhere in the same object.
Consider a generalised pointer concept such as a URL, it is easy to
check if two URL's result in the same location, but how do you provide a
complete ordering (I am not saying this is impossible, just that it is
non-trivial unless you know rather more than just that they are two
valid URL's)
Francis Glassborow Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]