Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.NS.USING_DIR

Using directive.

MISRA-C++ Rule 7-3-4 (required): using-directives shall not be used.

Rationale

using-directives add additional scopes to the set of scopes searched during name lookup. All identifiers in these scopes become visible, increasing the possibility that the identifier found by the compiler does not meet developer expectations.

using-declarations or fully qualified names restricts the set of names considered to only the name explicitly specified, and so are safer options.

Example

namespace NS1
{
   int32_t i1;
   int32_t j1;
   int32_t k1;
}

using namespace NS1; // Non-compliant

namespace NS2
{
   int32_t i2;
   int32_t j2;
   int32_t k2;
}

using NS2::j2; // Compliant

namespace NS3
{
   int32_t i3;
   int32_t j3;
   int32_t k3;
}

void f ()
{
   ++i1;
   ++j2;
   ++NS3::k3;
}

In the above, 'i1' is found via the using-directive. However, as a result of the using-directive, 'j1' and 'k1' are also visible. The using-declaration allows 'j2' to be found while 'i2' and 'k2' remain hidden. Finally, the qualified name 'NS3::k3' unambiguously refers to 'k3', and 'i3', 'j3' and 'k3' remain hidden to normal lookup.