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