Main Content

AUTOSAR C++14 Rule M7-3-6

Using-directives and using-declarations (excluding class scope or function scope using-declarations) shall not be used in header files

Description

Rule Definition

using-directives and using-declarations (excluding class scope or function scope using-declarations) shall not be used in header files.

Rationale

If using directives or declarations are present in a header file, the order in which the header is included might affect the final program results. It is a good practice to ensure that the program behavior does not depend on the order of inclusion of headers.

Polyspace Implementation

The checker flags using directives or declarations in header files.

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

expand all

Header File: headerUnits.h:

void checkValueForOverflows(int32_t);

namespace Units {
    void checkValueForOverflows(int8_t);
}

inline void convertToAcceptedUnit(int8_t originalValue) {
    checkValueForOverflows(originalValue);
}

Header File: headerAcceptedUnits.h:

namespace Units {
    
}

using namespace Units; //Noncompliant

Source file that includes previous two headers:

#include <cstdint>
#include "headerUnits.h"
#include "headerAcceptedUnits.h"

int8_t convert(int8_t meterReading) {
    convertToAcceptedUnit(meterReading);
}

In this example, the using directive in the header file headerAcceptedUnits.h exposes the namespace Units. This directive plus the inlined function in the header makes the order of inclusion of headerUnits.h and headerAcceptedUnits.h important for the final program execution.

  • If headerAcceptedUnits.h is included before headerUnits.h, the function convertToAcceptedUnit() invokes the function checkValueForOverflows(int32_t).

  • If headerAcceptedUnits.h is included after headerUnits.h, as shown above, the function convertToAcceptedUnit() invokes the function checkValueForOverflows(int8_t).

Check Information

Group: Declaration
Category: Required, Automated

Version History

Introduced in R2019a