MISRA C++:2023 Rule 19.0.2
Description
Rule Definition
Function-like macros shall not be defined.
Rationale
Using functions has certain advantages over using function-like macros. For example:
You can pass a function address to another function.
You can overload functions and create templates.
Type checking is performed on function arguments and return values.
For a full list of advantages of using functions over macros, see the MISRA-C++:2023 standard.
Polyspace Implementation
Polyspace® reports a rule violation whenever a function-like macro is defined. The following are exceptions to the rule when used in the macro definition as their equivalent behavior is not possible to implement in a function:
__LINE__
__FILE__
__func__
The
#
or##
operators
Polyspace does not report a violation when a function-like macro is defined as
(void)(X)
. These kinds of macros are typically used with a
function as an input. For example, you can use such a macro to explicitly cast the
return value of a function to void
. The use of this macro cannot be
replicated by a function.
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 R2024b