Note: Tested using Visual C++ 6.0 and VC++ .Net, Win32 empty console mode application. g++ (GNU C++) example is given at the end of this Module. Source code for the program examples is available inC++ Namespace source codes.
|
| 23.1 The Namespace
namespaceindentifier { namespace body }
namespace NewOne { int p; long q }
NewOne::p; NewOne::q;
|
// a namespace with using directive
#include <iostream>
using namespace std;
namespace SampleOne
{
float p = 10.34;
}
namespace SampleTwo
{
using namespace SampleOne;
float q = 77.12;
namespace InSampleTwo
{
float r = 34.725;
}
}
int main()
{
// this directive gives you everything declared in SampleTwo
using namespace SampleTwo;
// this directive gives you only InSampleTwo
using namespace SampleTwo::InSampleTwo;
// local declaration, take precedence
float p = 23.11;
cout<<"p = "<<p<<endl;
cout<<"q = "<<q<<endl;
cout<<"r = "<<r<<endl;
return 0;
}

Another program example.
// a namespace without using directive
#include <iostream>
using namespace std;
namespace NewNsOne
{
// declare namespace NewNsOne variable
int p = 4;
// declare namespace NewNsOne function
int funct(int q);
}
namespace NewNsTwo
{
// declare namespace NewNsTwo variable
int r = 6;
// declare namespace NewNsTwo function
int funct1(int numb);
// declare nested namespace
namespace InNewNsTwo
{
// declare namespace InNewNsTwo variable
long tst = 20.9456;
}
}
int main()
{
// the following four lines of code will generate error
// because it is not at global scope...
// namespace local
// {
// int k;
// }
cout<<"NewNsOne::p is "<<(NewNsOne::p)<<endl;
cout<<"NewNsTwo::r is "<<(NewNsTwo::r)<<endl;
cout<<"NewNsTwo::InNewNsTwo::tst is"<<(NewNsTwo::InNewNsTwo::tst)<<endl;
return 0;
}
Output:

23.1.1 Namespace Alias
Alternative name can be used to refer to a namespace identifier. An alias is useful when you need to simplify the long namespace identifier.
A program example:
// namespace alias
#include <iostream>
using namespace std;
namespace TheFirstLongNamespaceSample
{
float p = 23.44;
namespace TheNestedFirstLongNamespaceSample
{ int q = 100; }
}
// an alias namespace
namespace First = TheFirstLongNamespaceSample;
// use access qualifier to alias a nested namespace
namespace Second = TheFirstLongNamespaceSample::TheNestedFirstLongNamespaceSample;
int main()
{
using namespace First;
using namespace Second;
cout<<"p = "<<p<<endl;
cout<<"q = "<<q<<endl;
return 0;
}
23.1.2 Namespace Extension
|
// a namespace extension
// cannot be compiled, no output, just sample code
// original namespace
namespace One
{
// declare namespace One variable
int p;
int q;
}
namespace Two
{
float r;
int s;
}
// namespace extension of the One
namespace One
{
// declare namespace One function
void funct1(void);
int funct2(int p);
}
int main()
{
return 0;
}
// no output
Another program example.
// namespace extension
#include <iostream>
using namespace std;
struct SampleOne
{ };
struct SampleTwo
{ };
// original namespace
namespace NsOne
{
// original function...
void FunctOne(struct SampleOne)
{ cout<<"Processing the struct argument..."<<endl; }
}
using NsOne::FunctOne; // using-declaration...
// extending the NsOne namespace
namespace NsOne
{
// overloaded function...
void FunctOne(SampleTwo&)
{ cout<<"Processing the function argument..."<<endl; }
}
int main()
{
SampleOne TestStruct;
SampleTwo TestClass;
FunctOne(TestStruct);
// the following function call fails because there are
// no overloaded version for this one
// FunctOne(TestClass);
return 0;
}

Use the keyword namespace without identifier before the closing brace. This can be superior alternative to the use of the global static variable declaration.
Each identifier that is enclosed within an unnamed namespace is unique within the translation unit in which the unnamed namespace is defined.
The syntax:
namespace { namespace_body }
Behaves as if it were replaced by:
namespaceunique { namespace_body}
using namespace unique;
A program example:
// anonymous or unnamed namespace
#include <iostream>
using namespace std;
// anonymous namespace
namespace
{
int p = 1; // unique::p
}
void funct1()
{
++p; // unique::++p
}
namespace One
{
// nested anonymous namespace
namespace
{
int p; // One::unique::p
int q = 3; // One::unique::q
}
}
// using-declaration
using namespace One;
void testing()
{
// ++p; // error, unique::p or One::unique::p?
// One::++p; // error, One::p is undefined
cout<<"++q = "<<++q<<endl;
}
int main()
{
testing();
return 0;
}

There are several methods to access namespace elements as listed below:
Using explicit access qualification
Using directive
Using declaration
This method is useful when you wan to access several or all the members of a namespace.
This using-directive specifies that all identifiers in a namespace are in scope at the point that the using-directive statement is made.
It is also transitive; this means that when you apply the using directive to a namespace that contains using directive within itself, you get access to those namespaces as well.
A program example:
// using directive
#include <iostream>
using namespace std;
namespace One
{ float p = 3.1234; }
namespace Two
{
using namespace One;
float q = 4.5678;
namespace InTwo
{ float r = 5.1234; }
}
int main()
{
// this using directive gives you all declared in Two
using namespace Two;
// this using directive gives you only the InTwo namespace
using namespace Two::InTwo;
// this is local declaration, it takes precedence
// comment this code and re run this program, see the different...
float p = 6.12345;
cout<<"p = "<<p<<endl;
cout<<"q = "<<q<<endl;
cout<<"r = "<<r<<endl;
return 0;
}

You can access namespace elements individually by applying the using-declaration.
Here, you add the declared identifier to the local namespace. The syntax is:
using::unqualified-identifier;
A program example:
// using declaration
// function funct2() defined in two different namespaces
#include <iostream>
using namespace std;
namespace One
{
float funct1(float q)
{ return q; }
// first funct2() definition
void funct2()
{ cout<<"funct2() function, One namespace..."<<endl; }
}
namespace Two
{
// second funct2() definition
void funct2()
{ cout<<"funct2() function, Two namespace..."<<endl; }
}
int main()
{
// the using declaration identifies the desired version of funct2()
using One::funct1; // becomes qualified identifier
using Two::funct2; // becomes qualified identifier
float p = 4.556; // local declaration, takes precedence
cout<<"First p value, local = "<<p<<endl;
p = funct1(3.422);
cout<<"Second p value, by function call = "<<p<<endl;
funct2();
return 0;
}

tenouk fundamental of C++ object oriented tutorial
Source code for the program examples is available inC++ Namespace source codes.
Check thebest selling C / C++, STL and UML books at Amazon.com.
Acomplete C++ Standard Library documentation that includes STL.
Standards: The C/C++ standards references (ISO/IEC is covering ANSI and is more general):
a. ISO/IEC 9899 (ISO/IEC 9899:1999) - C Programming language.
b. ISO/IEC 9945-1:2003 POSIX standard.
c. ISO/IEC 14882:1998 on the programming language C++.
d. ISO/IEC 9945:2003, The Single UNIX Specification, Version 4.