home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sunic!hagbard!loglule!jbn
- From: jbn@lulea.trab.se (Johan Bengtsson)
- Newsgroups: comp.std.c++
- Subject: Re: Pointer comparisons and templates
- Message-ID: <5438@holden.lulea.trab.se>
- Date: 16 Dec 92 16:35:41 GMT
- References: <1992Dec15.222952.17059@lth.se>
- Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
- Lines: 69
- X-Newsreader: TIN [version 1.1 + PL8]
-
- Dag Bruck (dag@bellman.control.lth.se) wrote:
- : In <comp.std.c++> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
- : > You could always write the progam in terms of cmp(T,T),
- : >not <. The it would work for pointers too, you would just
- : >have to define cmp(T,T) for each type, including pointers,
-
- : I think that would be a serious effort, considering how many different
- : types of pointers you have in an application.
-
- : > template<class T> int cmp(T a1, T a2){return a1<a2;}
- : > int cmp(void* a1, void* a2){return ptrcmp(a1,a2)<0;}
-
- : I don't think this one flies. For example,
-
- I suggested a similar template in a previous response. Jerry Schwarz
- pointed out in private mail that it doesn't work. Only _exact_ type
- matches on functions are preferred over function templates, so all
- pointers except void* would end up calling the template.
-
- : int* p;
- : int* q;
- : cmp(p, q);
-
- : probably causes a template overloading ambiguity.
-
- Well, no. Since there is no trivial conversion from int* to void*,
- the template is preferred. The _standard_ conversion int* --> void*
- is not considered.
-
- : You have to
- : explicitly define a cmp() for every type of pointer you have, I think.
-
- So it seems. And in practice it would be too error-prone to do that.
-
- Jerry also said (I hope he doesn't mind me posting it):
- >
- > You could write
- >
- > template<class T> inline int isless(T* a, T* b) {
- > return ptrcmp(a,b) ;
- > }
- >
- > but then you have duplicate definitions of isless(int*,int*),
- > which isn't allowed.
-
- So this is a general limitation for C++ templates then:
- Using templates you can handle one unbounded set of types, but never
- two unbounded sets, at least not with the same template name.
-
- Dag's orignal claim that it is impossible to create templates
- that use "<" for all objects and ptrcmp() for all pointers seems
- to be valid.
-
- Using a bit of trickery, it is possible to create a template that
- works for pointers (ptrcmp) and objects (<), _except_ for classes
- that define automatic conversion to pointers (Dag found that one).
- I tested this, but won't describe the details here. Mail me if
- you are interested.
-
- : > The real problem cannot be solved at all, namely, for
- : >a user define < or cmp, ensuring it is a total order.
-
- This is a good point (IMHO).
-
- --
- --------------------------------------------------------------------------
- | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden |
- | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490 |
- --------------------------------------------------------------------------
-