Topic: Why pointers to members don't have a comparision operator?


Author: richard@ex-parrot.com
Date: Tue, 26 Apr 2005 00:23:14 CST
Raw View
Hyman Rosen wrote:
> kerzum@mail.ru wrote:
> > Why pointers to members of the same type cannot be compared with
'<'
> > operator?
>
> Just write your own comparison operator. A plain memcmp should do.
> There's no standard one because as you yourself point out there is
> no natural ordering.

Is it guaranteed that memcmp'ing two pointers to the same member will
return zero?  For example, can a pointer-to-member contain padding
bytes with undefined content, or bytes that are only used for certain
types of member?

--
Richard Smith

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ron@spamcop.net (Ron Natalie)
Date: Tue, 26 Apr 2005 20:54:16 GMT
Raw View
richard@ex-parrot.com wrote:

>
> Is it guaranteed that memcmp'ing two pointers to the same member will
> return zero?  For example, can a pointer-to-member contain padding
> bytes with undefined content, or bytes that are only used for certain
> types of member?

Besides, if the inheritance tree is complex enough (i.e., virtual
inheritance), it's not even sure that there is a strict ordering on
all possible pointers to member.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kuyper@wizard.net
Date: Wed, 27 Apr 2005 01:07:27 CST
Raw View
richard@ex-parrot.com wrote:
> Hyman Rosen wrote:
> > kerzum@mail.ru wrote:
> > > Why pointers to members of the same type cannot be compared with
> '<'
> > > operator?
> >
> > Just write your own comparison operator. A plain memcmp should do.
> > There's no standard one because as you yourself point out there is
> > no natural ordering.
>
> Is it guaranteed that memcmp'ing two pointers to the same member will
> return zero?  For example, can a pointer-to-member contain padding
> bytes with undefined content, or bytes that are only used for certain
> types of member?

The standard contains no wording that prohibits unused bytes, though in
this case I'd think that unused bits is a more plausible possibility.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kerzum@mail.ru
Date: Wed, 13 Apr 2005 00:54:56 CST
Raw View
Why pointers to members of the same type cannot be compared with '<'
operator?
I do not mean some particular order, just stable on any given
architecture/compiler.
Is there any obscure reason for that? As far as I see, pointers to
members are always
implemented either as
1. An offset from the beginning of the object
2. vtable entry, which is really an offset from the beginning of vtable
So this comparison could be easily implemented, for example
'1' preceding '2' and with arithmetic comparison within every of these
2 classes.
This will put no performance penalty on the code that does not use this
feature.
You will probably argue that this feature is completely useless, but my
question has been
raised from the real-world need - to use pointers to members as the
keys in associative containers.
Alternatively, std::less could be extended to cover the issue.
Users willing to use pointers to members as keys have no access to
their implementation
and are forced to use unsafe tricks with casts. So, won't it be the
most simple and convenient solution
to remove such limitation from the language core?

--
Peter A. Kerzum

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Wed, 13 Apr 2005 13:12:17 GMT
Raw View
kerzum@mail.ru wrote:
> Why pointers to members of the same type cannot be compared with '<'
> operator?

I guess because that would imply restrictions on the implementation
about the class layout, especially in the most complex cases such as
multiple virtual inheritance. You have to understand that the language
designers tried hard to give implementors the most possibile freedom
about the way objects are stored in memory. For example, in the standard
there is no reference to vtables, so your assumption that virtual
functions are implemented through vtables is shaky ground. I know that
most (if not all) current compilers use vtables, but who knows if
tomorrow some ultra-smart-optimizing-compiler-linker(tm) finds a
different way to achieve the same behaviour?

Alberto

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: hyrosen@mail.com (Hyman Rosen)
Date: Wed, 13 Apr 2005 13:11:58 GMT
Raw View
kerzum@mail.ru wrote:
> Why pointers to members of the same type cannot be compared with '<'
> operator?

Just write your own comparison operator. A plain memcmp should do.
There's no standard one because as you yourself point out there is
no natural ordering.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]