MISRA C++:2008 Rule 16-0-6
In the definition of a function-like macro, each instance of a parameter shall be enclosed in parentheses, unless it is used as the operand of # or ##
Description
Rule Definition
In the definition of a function-like macro, each instance of a parameter shall be enclosed in parentheses, unless it is used as the operand of # or ##.
Rationale
When you invoke function-like macros, the compiler expands the macro by replacing its parameters with the tokens. Then the compiler substitutes the expanded macro into the code. This expansion and substitution process does not take precedence of operation into account. The function-like macros might produce unexpected results if their parameters are not enclosed in parenthesis. For instance, consider this function-like macro:
#define dustance_from_ten(x) x>10? x-10:10-x
a-b>10: a-b-10:10-a-b
10-a-b
is equivalent to 10-(a+b)
instead of the
intended distance 10-(a-b)
. This unexpected behavior might result in
errors and bugs. To avoid such unexpected behaviors, enclose parameters of a function-like
macro in parentheses.The exception to this rule is when a parameter is used as an operand of
#
or ##
.
Polyspace Implementation
Polyspace® flags function-like macro definitions if the parameters are not enclosed in
parenthesis. Polyspace does not flag unparenthesized parameters if they are preceded by the operators
.
, ->
, or the characters #
,
##
.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Preprocessing Directives |
Category: Required |
Version History
Introduced in R2013b