Qt-Autosar-M16.2.3¶
Include guards shall be provided
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
The include guard shall use one of the following two forms:
<start-of-file>
// Comments allowed here
#if !defined ( identifier )
#define identifier
// Contents of file
#endif
<end-of-file>
<start-of-file>
// Comments allowed here
#ifndef identifier
#define identifier
// Contents of file
#endif
<end-of-file>
Rationale
When a translation unit contains a complex hierarchy of nested header files, it is possible for a particular header file to be included more than once. This can be, at best, a source of confusion. If this multiple inclusion leads to multiple or conflicting definitions, then this can result in undefined or erroneous behaviour.
These forms are mandated to facilitate checking.
Example
// file.h
#ifndef fiLE_H
#endif
// file.cc
#include "file.h"
#define fiLE_H // Non-compliant
Note
For legal reasons, this rule’s description is not part of the public documentation.