0 out of 2 rated this helpful - Rate this topic

using Directive (C++)

The using directive allows the names in a namespace to be used without the namespace-name as an explicit qualifier. Of course, the complete, qualified name can still be used to improve readability.

using namespace [::] [ nested-name-specifier ] namespace-name

Note the difference between the using directive and the using declaration : the using declaration allows an individual name to be used without qualification, the using directive allows all the names in a namespace to be used without qualification.

If a local variable has the same name as a namespace variable, the namespace variable is hidden. It is an error to have a namespace variable with the same name as a global variable.

Note Note

Put your using directive at the beginning of the source code file to reduce the potential for unexpected behavior with IntelliSense.

The std namespace

The ANSI/ISO C++ standard requires you to explicitly declare the namespace in the standard library. For example, when using iostream, you must specify the namespace of cout in one of the following ways:

  • std::cout (explicitly)

  • using std::cout (using declaration)

  • using namespace std (using directive)

/clr

The following sample shows how to allow names in a .NET Framework base class library namespace to be used without the namespace-name as an explicit qualifier.

// using_directive.cpp
// compile with: /c /clr
using namespace System::Reflection;
[assembly:AssemblyDescriptionAttribute("test")];
// using_directive2.cpp
// compile with: /EHsc
#include <iostream>

int main() {
   std::cout << "Hello ";
   using namespace std;
   cout << "World." << endl;
}
Hello World.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Scope Semantic
This doc tell us only the syntaks not the semantics (meaning).
What is the scope of the directive, the the rest from statement
or just the next statement or maybe this block
like a local variable?