Rule Definition
While it is legal to place #define or #undef directives anywhere in a source file, placing them outside of the global namespace is misleading as their scope is not restricted. This may be inconsistent with developer expectations.
Remediation
Define the macros in the header
Violation Code Sample
namespace NS
{
#define FOO // Non-compliant
#undef FOO // Non-compliant
}
#endif
Fixed Code Sample
#ifndef MY_HDR
#define MY_HDR // Compliant
Reference
MISRA 2008 C++, 16-0-2: Macros shall only be #define’d or #undef’d in the global namespace.
Related Technologies
Technical Criterion
Programming Practices - Unexpected Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.