MISRA C++:2008 Rule 16-0-2
Macros shall only be #define 'd or #undef 'd in the global namespace
Description
Rule Definition
Macros shall only be #define 'd or #undef 'd in the global namespace.
Rationale
If you define or undefine macros in a local namespace, you might expect the macro to be
valid only in the local namespace. But macros do not follow the scoping mechanism. Instead,
the compiler replaces all occurrences of a macro by its defined value beginning at the
#define
statement until the end of file or until the macro is
redefined. This behavior of macros might be contrary to developer expectation and might
cause logic errors that result in bugs.
Polyspace Implementation
Polyspace® flags a #define
or #undef
statement that
is placed within a block instead of in the global namespace.
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