C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
kerzum@mail.ru
Guest





PostPosted: Wed Apr 13, 2005 6:54 am    Post subject: Why pointers to members don't have a comparision operator? Reply with quote



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 ]

Back to top
Hyman Rosen
Guest





PostPosted: Wed Apr 13, 2005 1:11 pm    Post subject: Re: Why pointers to members don't have a comparision operato Reply with quote



[email]kerzum (AT) mail (DOT) ru[/email] wrote:
Quote:
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 ]


Back to top
Alberto Barbati
Guest





PostPosted: Wed Apr 13, 2005 1:12 pm    Post subject: Re: Why pointers to members don't have a comparision operato Reply with quote



[email]kerzum (AT) mail (DOT) ru[/email] wrote:
Quote:
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 ]


Back to top
richard@ex-parrot.com
Guest





PostPosted: Tue Apr 26, 2005 6:23 am    Post subject: Re: Why pointers to members don't have a comparision operato Reply with quote


Hyman Rosen wrote:
Quote:
kerzum (AT) mail (DOT) 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 ]


Back to top
Ron Natalie
Guest





PostPosted: Tue Apr 26, 2005 8:54 pm    Post subject: Re: Why pointers to members don't have a comparision operato Reply with quote

[email]richard (AT) ex-parrot (DOT) com[/email] wrote:

Quote:

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 ]


Back to top
kuyper@wizard.net
Guest





PostPosted: Wed Apr 27, 2005 7:07 am    Post subject: Re: Why pointers to members don't have a comparision operato Reply with quote

[email]richard (AT) ex-parrot (DOT) com[/email] wrote:
Quote:
Hyman Rosen wrote:
[email]kerzum (AT) mail (DOT) ru[/email] 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 ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.