AutosarC++19_03-M16.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 ##
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
If parentheses are not used, then the operator precedence may not give the desired results when the preprocessor substitutes the macro into the code.
Within a definition of a function-like macro, the arguments shall be enclosed in parentheses.
Example
Define an abs function using:
#define abs(x) (((x) >= 0) ? (x) : -(x)) // Compliant
and not:
#define abs(x) ((x >= 0) ? x : -x) // Non-compliant
Consider what happens if the second, incorrect, definition is substituted into the expression:
z = abs( a - b );
giving:
z = ((a - b >= 0) ? a - b : -a – b);
The sub-expression -a - b is equivalent to (-a)-b rather than -(a-b) as intended. Putting all the parameters in parentheses in the macro definition avoids this problem.
#define subs(x) a ## x // Compliant
Note
For legal reasons, this rule’s description is not part of the public documentation.