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
Remarks
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.
备注
Putt 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")];
Example
// using_directive2.cpp
// compile with: /EHsc
#include <iostream>
int main() {
std::cout << "Hello ";
using namespace std;
cout << "World." << endl;
}
Hello World.