This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: min/max macros
- To: egcs at cygnus dot com
- Subject: Re: min/max macros
- From: Paul Koning <pkoning at xedia dot com>
- Date: Fri, 12 Dec 1997 10:50:44 -0500
- References: <01BD0625.FDFCC640@bill.icdata.com>
- Reply-To: egcs at cygnus dot com
>>>>> "Bill" == Bill Ahlbrandt <bahlbr@icdata.com> writes:
Bill> I have noticed that these macros are not always available and
Bill> not always in the same place. Specifically, with egcs, they
Bill> seem to be in curses.h
Bill> I "coded" my own and used them as follows:
Bill> #define max(a,b) (((a) > (b)) ? (a) : (b)) #define min(a,b)
Bill> (((a) < (b)) ? (a) : (b))
Bill> After discovering that max in particular was not yielding the
Bill> desired results, I coded these macros as functions. All of my
Bill> problems went away.
It's always dangerous to create macros that use a macro argument more
than once. You will get the wrong results if the macro is called with
an actual argument that has side effects (e.g., max (i++, j++) ).
Apart from that, functions have the benefit of type checking.
For these reasons, I tend to use functions when I can. You can
declare them "static inline" to get the benefits without any
performance penalties.
paul