Namespaces - Examples


Annotated version (Answers. Don't look here until you've determined which symbols are being referenced or which code is illegal.)

This code is fine:


#include <iostream> 
using namespace std;

namespace Stuff
{
  int foo = 11;
  int bar = 12;
}

int foo = 21;
int bar = 22;

void f1(void)
{
  cout << foo << endl;
  cout << Stuff::foo << endl;
}

int main(void)
{
  cout << foo << endl;        
  cout << ::foo << endl;      
  cout << Stuff::foo << endl; 
  return 0;
}

Notice the using directive and its placement:

namespace Stuff
{
  int foo = 11;
  int bar = 12;
}

using namespace Stuff;
                        
                        
int foo = 21;
int bar = 22;

void f1(void)
{
  cout << foo << endl;        
  cout << Stuff::foo << endl; 
}

int main(void)
{
  cout << foo << endl;        
  cout << ::foo << endl;      
  cout << Stuff::foo << endl; 
  return 0;
}

The same using directive is moved and produces different results:

namespace Stuff
{
  int foo = 11;  
  int bar = 12;  
}

int foo = 21;  
int bar = 22;  

void f1(void)
{
  cout << foo << endl;        
  cout << Stuff::foo << endl; 
}

using namespace Stuff;  
                        
                        
int main(void)
{
  cout << foo << endl;        
  cout << ::foo << endl;      
  cout << Stuff::foo << endl; 
  return 0;
}

The same using directive is placed within a function (scope is only in the function):

namespace Stuff
{
  int foo = 11;  
  int bar = 12;  
}

int foo = 21;  
int bar = 22;  

void f1(void)
{
  using namespace Stuff;  
                          
                        
  cout << foo << endl;        
  cout << Stuff::foo << endl; 
}

int main(void)
{
  cout << foo << endl;        
  cout << ::foo << endl;      
  cout << Stuff::foo << endl; 
  return 0;
}

The same using directive is placed in main after some code:

namespace Stuff
{
  int foo = 11;  
  int bar = 12;  
}

int foo = 21;  
int bar = 22;  

void f1(void)
{
  cout << foo << endl;        
  cout << Stuff::foo << endl; 
}

int main(void)
{
  cout << foo << endl;        
  cout << ::foo << endl;      
  cout << Stuff::foo << endl; 

  using namespace Stuff;  
                          
                        
  cout << foo << endl;    
  
  int foo = 41;           
  cout << foo << endl;    
  cout << ::foo << endl;  

  return 0;
}

Now, we are employing the using declaration, and we get very different behavior:

namespace Stuff
{
  int foo = 11;  
  int bar = 12;  
}

using Stuff::foo; 
                  

int foo = 21;  
int bar = 22;  

void f1(void)
{
  cout << foo << endl;        
  cout << Stuff::foo << endl; 
}

void f2(void)
{
  int foo = 31;               
  cout << foo << endl;        
  cout << Stuff::foo << endl; 
}

int main(void)
{
  cout << foo << endl;        
  cout << ::foo << endl;      
  cout << Stuff::foo << endl; 

  int foo = 41;           
  cout << foo << endl;    
  cout << ::foo << endl;  

  return 0;
}

Move using declaration into a new scope (in main):

namespace Stuff
{
  int foo = 11;  
  int bar = 12;  
}

int foo = 21;  
int bar = 22;  

void f1(void)
{
  cout << foo << endl;        
  cout << Stuff::foo << endl; 
}

void f2(void)
{
  int foo = 31;               
  cout << foo << endl;        
  cout << Stuff::foo << endl; 
}

int main(void)
{
  cout << foo << endl;        
  cout << ::foo << endl;      
  cout << Stuff::foo << endl; 
  
  using Stuff::foo; 

  int foo = 41;           
  cout << foo << endl;    
  cout << ::foo << endl;  
  
  return 0;
}

Annotated version