Talk:Handy macros (C)

From LiteratePrograms
Jump to: navigation, search

[edit] Avoiding side-effects in macros

The possibly most well-known problem with C macros is that the arguments can be evaluated multiple times, giving surprising side-effects. A solution using temporary variables has been introduced to this article. I think this solution has some problems:

  • The macros are IMO less readable
  • Using thme involves more work, as the user has to define the temporary variables
  • Using a macro in an argument to the same macro (bounds(n, 3, bounds(nn, 4, 7))) can give surprising results

I suggest we rather use the lazy method of ignoring the side-effects and leave this to the user of the macro. The macros should probably be renamed to all uppercase, to make it clear.

If a "macro" acting like a function is necessary, an inlined function should probably be used. Inlining, while not available in C89, is available in the C99 standard and as an extension in some compilers. Ahy1 03:49, 13 July 2007 (PDT)