Main Content

AUTOSAR C++14 Rule M7-3-4

Using-directives shall not be used

Description

Rule Definition

using-directives shall not be used.

Rationale

using directives can inadvertently expose more variables to name lookup than you intend. The wider scope for name lookup increases the likelihood of a variable being unintentionally used. For instance:

namespace NS {
    int i;
    int j;
}
using namespace NS;
exposes both NS::i and NS::j to name lookup, but you might have intended to use only one of the variables.

It is preferable to only expose variables that you intend to use or refer to variables by their fully qualified name when you use them. For instance, in the previous example, if you use the variable NS::i, expose only this variable to name lookup:

using NS::i;
or refer to NS::i instead of i in all instances.

Polyspace Implementation

The rule checker reports a violation on using directives. These directives contain the keyword using followed by the keyword namespace and the name of a namespace. The using directives make all members of a namespace available to the current scope.

The checker does not flag using declarations. These declarations also contain the keyword using but not the keyword namespace. The using declarations only expose specific members of a 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

expand all

namespace currentDataModel {
    int nodes;
    int count;
}
using namespace currentDataModel; //Noncompliant

namespace lastDataModel {
    int nodes;
    int count;
}
using lastDataModel::count; //Compliant

In this example, the using directive that exposes all members of the namespace currentDataModel violates the rule.

Check Information

Group: Declaration
Category: Required, Automated

Version History

Introduced in R2019a