home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / std / cplus / 1812 < prev    next >
Encoding:
Internet Message Format  |  1992-12-16  |  2.9 KB

  1. Path: sparky!uunet!mcsun!sunic!hagbard!loglule!jbn
  2. From: jbn@lulea.trab.se (Johan Bengtsson)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Pointer comparisons and templates
  5. Message-ID: <5438@holden.lulea.trab.se>
  6. Date: 16 Dec 92 16:35:41 GMT
  7. References: <1992Dec15.222952.17059@lth.se>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 69
  10. X-Newsreader: TIN [version 1.1 + PL8]
  11.  
  12. Dag Bruck (dag@bellman.control.lth.se) wrote:
  13. : In <comp.std.c++> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  14. : >    You could always write the progam in terms of cmp(T,T),
  15. : >not <. The it would work for pointers too, you would just
  16. : >have to define cmp(T,T) for each type, including pointers,
  17.  
  18. : I think that would be a serious effort, considering how many different
  19. : types of pointers you have in an application.
  20.  
  21. : >    template<class T> int cmp(T a1, T a2){return a1<a2;}
  22. : >    int cmp(void* a1, void* a2){return ptrcmp(a1,a2)<0;}
  23.  
  24. : I don't think this one flies.  For example,
  25.  
  26. I suggested a similar template in a previous response.  Jerry Schwarz
  27. pointed out in private mail that it doesn't work.  Only _exact_ type
  28. matches on functions are preferred over function templates, so all
  29. pointers except void* would end up calling the template.
  30.  
  31. :     int* p;
  32. :     int* q;
  33. :     cmp(p, q);
  34.  
  35. : probably causes a template overloading ambiguity.
  36.  
  37. Well, no.  Since there is no trivial conversion from int* to void*,
  38. the template is preferred.  The _standard_ conversion int* --> void*
  39. is not considered.
  40.  
  41. : You have to
  42. : explicitly define a cmp() for every type of pointer you have, I think.
  43.  
  44. So it seems.  And in practice it would be too error-prone to do that.
  45.  
  46. Jerry also said (I hope he doesn't mind me posting it):
  47. > You could write
  48. >     template<class T> inline int isless(T* a, T* b) {
  49. >          return ptrcmp(a,b) ;
  50. >         }
  51. > but then you have duplicate definitions of isless(int*,int*),
  52. > which isn't allowed.
  53.  
  54. So this is a general limitation for C++ templates then:
  55. Using templates you can handle one unbounded set of types, but never
  56. two unbounded sets, at least not with the same template name.
  57.  
  58. Dag's orignal claim that it is impossible to create templates
  59. that use "<" for all objects and ptrcmp() for all pointers seems
  60. to be valid.
  61.  
  62. Using a bit of trickery, it is possible to create a template that
  63. works for pointers (ptrcmp) and objects (<), _except_ for classes
  64. that define automatic conversion to pointers (Dag found that one).
  65. I tested this, but won't describe the details here.  Mail me if
  66. you are interested.
  67.  
  68. : >    The real problem cannot be solved at all, namely, for
  69. : >a user define < or cmp, ensuring it is a total order.
  70.  
  71. This is a good point (IMHO).
  72.  
  73. -- 
  74. --------------------------------------------------------------------------
  75. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  76. | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
  77. --------------------------------------------------------------------------
  78.