Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

An unsafe macro function is one that evaluates a parameter more than once in the code expansion, or potentially never evaluates the parameter at all. Never invoke an unsafe macro with arguments containing an assignment, increment, decrement, volatile access, or other side effects including function calls which may cause side effects. Any input or output is also a side effect, even though it might be accomplished through function calls or volatile access. Thus input and output must similarly be avoided in arguments to unsafe macros.

The documentation for unsafe macros must warn about putting side effects on the invocation, and the responsibility is on the programmer using the macro. Because of the risks associated with their use, it is recommended that you avoid the creation of unsafe macro functions. See also PRE00-A. Prefer inline or static functions to function-like macros.

The assert() macro is an excellent example of an unsafe macro. Its argument may be evaluated once or not at all, depending on the NDEBUG macro. For more information, see [EXP39-C. Avoid side effects in assertions}.

Non-Compliant Coding Example

...