home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15819 < prev    next >
Encoding:
Text File  |  1992-11-05  |  1.5 KB  |  41 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!pipex!warwick!bham!bhamvx!mccauleyba
  3. From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
  4. Subject: Re: Making qsort type-safe
  5. Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
  6. Message-ID: <1992Nov5.125947.1@vax1.bham.ac.uk>
  7. Date: Thu, 5 Nov 1992 12:59:47 GMT
  8. Lines: 29
  9. References: <1992Nov4.162131.10289@cs.brown.edu>
  10. Organization: University of Birmingham
  11.  
  12. In article <1992Nov4.162131.10289@cs.brown.edu>, sdm@cs.brown.edu (Scott Meyers) writes:
  13. > So I thought of doing this:
  14. >     typedef int (*CMP)(const void*, const void*);
  15. >     template<class T>
  16. >     inline
  17. >     void safeQSort(T array[], int arraySize,
  18. >                    int (*cmp)(const T *val1, const T *val2)) 
  19. >     {
  20. >         qsort(array, arraySize, sizeof(T), (CMP) cmp);
  21. >     }
  22. > But casts make me queasy, especially so after the mail I got telling me
  23. > about bit pattern conversions when types change.  So -- is this a safe
  24. > thing to do, or does the devil lurk within?
  25.  
  26. Other responses address the possibility that you could write the wholw qsort in
  27. C++. In answer to your direct question yes the devil does lurk within. It is
  28. possible for a conforming compiler to generate code from your source that will
  29. not work - it is however very unlikely. (I'm sure loads of people will now tell
  30. me of compilers that would fail in this case).
  31.  
  32.     \\   ( )  NO BULLSHIT! from BAM (Brian McCauley)          
  33.  .  _\\__[oo  ============
  34. .__/  \\ /\@
  35. .  l___\\     E-mail: B.A.McCauley@bham.ac.uk
  36.  # ll  l\\ 
  37. ###LL  LL\\
  38.