home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 20090 < prev    next >
Encoding:
Text File  |  1993-01-28  |  2.5 KB  |  65 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!europa.eng.gtefsd.com!emory!sol.ctr.columbia.edu!ira.uka.de!slsvaat!josef!kanze
  3. From: kanze@us-es.sel.de (James Kanze)
  4. Subject: Re: Function 'max' should have a prototype
  5. In-Reply-To: spencer@cats.ucsc.edu's message of 25 Jan 1993 23:36:38 GMT
  6. Message-ID: <KANZE.93Jan28194956@slsvdnt.us-es.sel.de>
  7. Sender: news@us-es.sel.de
  8. Organization: SEL
  9. References: <hrpctw.14@pnv.palm.cri.nz> <1k1ti6INNe6c@darkstar.UCSC.EDU>
  10. Date: 28 Jan 93 19:49:56
  11. Lines: 52
  12.  
  13. In article <1k1ti6INNe6c@darkstar.UCSC.EDU> spencer@cats.ucsc.edu
  14. (Michael Spencer) writes:
  15.  
  16. |> hrpctw@pnv.palm.cri.nz (Colin T. Wilson-Salt) writes:
  17.  
  18. |> >I can't seem to get the max() macro to work.  It works OK under C, but
  19. |> >under C++ it gives me the error "Function 'max' should have a prototype".
  20.  
  21. What is the "max() macro"?  In the text you posted (not fully quoted
  22. by your respondant), there is no such macro defined.  And you only
  23. include standard headers, so it's not in the header.
  24.  
  25. C should implicitly define the function, as though it had a prototype.
  26. This is one place where C++ differs, however; all functions must have
  27. a prototype.
  28.  
  29. |> >Should I give up and write a replacement max() function/macro?
  30.  
  31. |> Roll your own. It's a lot easier than trying to figure out the
  32. |> vagaries of compilers!  Try:
  33.  
  34. |> #define Minim(a,b)            ((a<b) ? a : b)
  35. |> #define Maxim(a,b)            ((a>b) ? a : b)
  36. |> #define InRange(a,lo,hi)      ((a<lo) ? 0 : ((a>hi) ? 0 : 1))
  37. |> #define RangeSet(a,lo,hi)     ((a<lo) ? (a = lo) : (a>hi) ? (a = hi) : a )
  38. |> #define CircleSet(a,lo,hi)    ((a<lo) ? (a = hi) : (a>hi) ? (a = lo) : a )
  39.  
  40. |> The last 3 will help avoid 'fence-post' errors.
  41.  
  42. The idea is good, but I don't like the implementation.  What happens
  43. in the following case, for example:
  44.  
  45.     int            maxSoFar = INT_MIN ;
  46.     int*            p = myTable ;
  47.     for ( int i = tableLength ; i > 0 ; i -- )
  48.         maxSoFar = Maxim( maxSoFar , *p ++ ) ;
  49.  
  50. You guessed it.  When p is larger than maxSoFar (which it will often
  51. be at the beginning of the table), it gets incremented twice in the
  52. loop.
  53.  
  54. BTW, I'm not recommending the above as good programming style.  But it
  55. is typical of the way a lot of people write C.
  56.  
  57. The solution would be to replace all of the above functions with
  58. inline's.  This way, the compiler guarantees that each parameter is
  59. evaluated once and only once per call.
  60. --
  61. James Kanze                             email: kanze@us-es.sel.de
  62. GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
  63. Conseils en informatique industrielle --
  64.                    -- Beratung in industrieller Datenverarbeitung
  65.