home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15939 < prev    next >
Encoding:
Internet Message Format  |  1992-11-08  |  2.2 KB

  1. Path: sparky!uunet!mcsun!uknet!doc.ic.ac.uk!agate!spool.mu.edu!darwin.sura.net!sgiblab!munnari.oz.au!mel.dit.csiro.au!mineng.dmpe.CSIRO.AU!dmssyd.syd.dms.CSIRO.AU!metro!otc!swdev!grahamd
  2. From: grahamd@swdev.research.otca.oz.au (Graham Dumpleton)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Debugging output
  5. Message-ID: <6706@otc.otca.oz>
  6. Date: 8 Nov 92 00:00:32 GMT
  7. References: <spuler.720931050@coral> <Bx9369.3zL@cs.uiuc.edu> <Bx93Mr.42r@cs.uiuc.edu>
  8. Sender: news@otc.otca.oz
  9. Organization: Technology Development Group, OTC Australia
  10. Lines: 57
  11.  
  12. In article <Bx93Mr.42r@cs.uiuc.edu>, pjl@cs.uiuc.edu (Paul Lucas) writes:
  13. > In <Bx9369.3zL@cs.uiuc.edu> pjl@cs.uiuc.edu (Paul Lucas) writes:
  14. > >In <spuler.720931050@coral> spuler@coral.cs.jcu.edu.au (David Spuler) writes:
  15. >
  16. > >*****>    This is overkill; the same thing can be achieved without
  17. > >    inventing another class:
  18. > >    #ifdef DEBUG
  19. > >    #    define    CDEBUG    if ( debug ) cerr
  20. > >    #else
  21. > >    #    define    CDEBUG    //
  22. > >    #endif
  23. > ****>    That only works if you have a C++-ignorant preprocessor, i.e., so
  24. >     it won't strip the // comment.  This is one instance, among others,
  25. >     why I would like to keep cpp //-ignorant; you can do all sorts of
  26. >     hokey stuff that way.
  27. > >    // ...
  28. > >    CDEBUG << "here\n";
  29. > >    This allows the flexibility of having debugging "togglable"
  30. > >    during development, presumeably by a command-line option, but
  31. > >    can be compiled-out in the final version.
  32.  
  33. Noting that cfront, and I imagine any 'good' compiler, will optmise away the
  34. body of an 'if' statement if given a condition which can be evaluated at compile
  35. time, you can say.
  36.  
  37.   #ifdef DEBUG
  38.   #define CDEBUG if (debug) cerr
  39.   #else
  40.   #define CDEBUG if (0) cerr
  41.   #endif
  42.  
  43. One problem with this though, which David Spuler pointed out to me when I told
  44. him about it, is that for safety you are best off writing
  45.  
  46.   #ifdef DEBUG
  47.   #define CDEBUG if (!debug) ; else cerr
  48.   #else
  49.   #define CDEBUG if (1) ; else cerr
  50.   #endif
  51.  
  52. If you didn't and some one wrote
  53.  
  54.   if (...)
  55.     CDEBUG << "here\n";
  56.   else
  57.     ...
  58.  
  59. you would end up with the 'else' matching the 'if' within the CDEBUG macro
  60. which could give some unexpected results.
  61.  
  62. -- 
  63. Graham Dumpleton (grahamd@swdev.research.otca.oz.au)
  64.