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