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

  1. Path: sparky!uunet!enterpoop.mit.edu!eru.mt.luth.se!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: <5376@miramon.lulea.trab.se>
  6. Date: 15 Dec 92 01:14:31 GMT
  7. References: <1992Dec14.075853.3399@lth.se>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 41
  10. X-Newsreader: TIN [version 1.1 + PL8]
  11.  
  12. Dag Bruck (dag@bellman.control.lth.se) wrote:
  13. : In the recent discussion on pointer comparisons, Fergus Hendersson
  14. : suggested adding a standard library function ptrcmp() instead of
  15. : extending the definitions of "<" and ">" for pointers.  I think we
  16. : also agreed on a reasonable relationship between ptrcmp() and the
  17. : relational operators.
  18.  
  19. : While I think ptrcmp() is better than nothing, I still think extending
  20. : the definition of the relational operators is far better.  Consider
  21. : this template class (the code may be wrong, I'm just inventing an
  22. : almost real example for the purpose of discussion):
  23.  
  24. [ class template needing total ordering on objects _and_ pointers deleted ]
  25.  
  26. : Now, here's the problem: this implementation works fine for all
  27. : element types that have an "operator < ()" including plain built-in
  28. : types and many user-defined types.  However, it is not guaranteed to
  29. : work with pointer elements for reasons we have discussed before.
  30.  
  31. : They way around this problem is to make a specialization for pointers:
  32.  
  33. Dag, there is another way:
  34.  
  35. Have the class template rely on a function template isless() instead,
  36. with a specialization for pointers:
  37.  
  38. template<class T> inline int isless(T a, T b)
  39. { return a < b; }
  40.  
  41. inline int isless( const void* a, const void* b )
  42. { return ptrcmp(a,b) < 0; }
  43.  
  44. That ought to do it.  NOTE: I don't yet have a compiler that does
  45. function templates, so this is untested.  Perhaps someone with a
  46. more real compiler can check that this really works?
  47.  
  48. -- 
  49. --------------------------------------------------------------------------
  50. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  51. | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
  52. --------------------------------------------------------------------------
  53.