c++ - More (void) sadness ... a more compelling example this time
- Matthew Wilson (36/36) Jun 11 2003 Consider the following example
Consider the following example #ifdef __SYNSOFT_DBS_OPNEW_THROWS_EXCEPTIONS void throw_bad_alloc(Size si) { // Do some throwing stuff } #else #define throw_bad_alloc(_si) ((void)0) #endif /* __SYNSOFT_DBS_OPNEW_THROWS_EXCEPTIONS */ //////////////////////////////////////////////////////////////////////////// //// void *operator new(Size si) { void *pv = Win_Alloc(si); if(pv == NULL) { throw_bad_alloc(si); } return pv; } When the compiler does not support throwing exceptions on mem exhaustion, throw_bad_alloc() is a macro that translates to ((void)si); in order to prevent the compiler from whitering about an unused argument. However, by (void) casting it, we precipitate the otherwise grand -wc warning. Naturally an alternative is to write throw_bad_alloc() as an empty inline function #else inline void throw_bad_alloc(Size /*si*/) {} #endif /* __SYNSOFT_DBS_OPNEW_THROWS_EXCEPTIONS */ which I am now doing (since I cannot wait), but in general this may not always be easy or desirable (people tend to like to not have to change code on big projects unless there are real bugs). Matthew
Jun 11 2003