Namespace – What’s the hell?(Part 1)

To be honest I use this term everyday; however I only understand that it stands for some standard prefix. Reference to a professional C++ book I want to deep more into this term. Hope I can learn something new and share them here.

Firstly, I want to share a common code script that uses namespace really often:

#include <iostream>

 using namespace std;

int main() {

int it;




Actually, cin and cout require std::cin and std::cout respectively, because cin and cout are functions inside Standard Library of C++. Namespace stands for vendors, for examples: there are two functions has the same name, let’s say: waka, one from library apple, another from google. Two functions do different jobs, so to avoid any conflict for compilation, namespaces are used to indicate which version of the function is needed (from apple or from google).

There are two ways to use namespace: declarations and directives:

using namespace1::var1;  //declarations

using namespace namespace1; // directives

Let’s reconsider about variable scope. In some extents, namespaces also define scopes. About variable or function scope, there are global, local scopes. Let’s examine the following example:

int global_var{5}; // declare a global variable
int main(){
int global_var{2}; // this variable hides the global variable that has the same name
std::cout<<global_var<<std::endl; // print out 2 – local variable
std::cout<<::global_var<<std::endl; // print out 5 – global variable

Now we consider declaration of namespace as following:

using nS1::var1;

There are some points need to remember:

  1. declaration namespace works for only particular variable (for above declaration – var1);
  2. make var1 become accessible in the region it is declared as local one.

These main point are introduced in the following example:

namespace nS1{
int var1{2}; // variable nS1::var1;
char var2{‘a’}; //variable nS1::var2;
using nS1::var2; // make nS1::var become global one
int var1{3}; // global variable var1
int main(){
using nS1::var1; // this var1 hides global var1
//int var1; // causes an error – redeclaration
int var2{3}; // this local variable hides the same name global
std::cout<<var1<<std::endl; //cout local var1
std::cout<<::var1<<std::endl; //cout global var1
std::cout<<::var2<<std::endl; //cout global var2 – nS1::var2
std::cout<<var2<<std::endl; //cout local var2

In the above example, we can see the relations between namespace declarations and scopes of the respective variables.

Using directive namespace make totally set identified. Let’s consider the following example:

namespace nS1{
int var1{2}; // variable nS1::var1;
char var2{‘a’}; //variable nS1::var2;
int var1{5}; //declare a global variable var1, set its value is 5
int main(){
using namespace nS1;
var1++; //this one is nS1::var1 increases 1, it does not override the global one
double var1{.5}; //this one hides nS1::var1
std::cout<<var1<<std::endl; //cout local var1
std::cout<<nS1::var1<<std::endl; //cout nS1::var1
std::cout<<::var1<<std::endl; //cout global var1

This code occurs an error in var1++ (cause an ambiguity). Why? The reason is directive namespace does not hide global variable as declaration, so the compiler does not know which var1 (nS1::var1 or ::var1) will be used.

*** There are more about transitiveness and unnamed namespace that I will talk about later.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s