Re: set of pointers
In message
<642614c3-c77d-4b10-9293-ce095dd66ec5@28g2000hsw.googlegroups.com>,
Lance Diduck <lancediduck@nyc.rr.com> writes
>On Feb 26, 10:31am, REH <spamj...@stny.rr.com> wrote:
>> Is it well defined to use a std::set of pointers? I ask because of
>> the restrictions on using relational operators with pointers. Does a
>> set of pointers suffer from the same restrictions, or does the
>> standard require it to be supported. I can't find any information one
>> way or the other in the standard or from other sources.
Look again...
>
>Since pointers implement operator< as required by std::set,
No. According to 5.9/2 [expr.rel], among other restrictions:
"If two pointers p and q of the same type point to different objects
that are not members of the same object or elements of the same array or
to different functions, or if only one of them is null, the results of
p<q, p>q, p<=q, and p>=q are unspecified."
Putting it loosely, the effect is that operator< is only defined for
pointers to elements of the same array, or (but not always) data members
of the same object.
But std::less (which is what std::set uses by default, not operator<) is
required by 20.3.3/8 to define an ordering on pointers even when
operator< does not.
So this is still true:
>then it is
>possible to use pointers.
>Note that what is being compared is the actual address being stored in
>the pointer, and not what it points to.
Not necessarily the "address". If the pointers aren't into the same
array, std::less may have to use compiler magic to define an ordering.
--
Richard Herring
|