This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: min/max macros


>>>>> "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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]