computer science II
c m s c 214  
f a l l   2 0 0 1  

Namespaces

© by Charles Lin. All rights reserved. You must receive written permission from Charles Lin to reproduce this webpage in any form.

Namespaces were introduced to the C++ language to prevent "namespace clashes". For example, suppose you wish to write to use some classes written by two software companies, and both of them produce a Math class. You want to use both of them, because each class has some useful function the other one doesn't provide.

You get ready to compile the code, and the compiler complains that there are two Math classes and won't let you use both. It seems like you're stuck.

This is one reason to use namespaces. It allows you to "qualify" each of the Math classes with a "namespace". Think of the namespace as a kind of brand name. So, if one company was called "SoftwareOne", they could create a namespace for their class, as:

namespace SoftwareOne {

  class Math
  {
   ...
  };
}
and if the other software company is called "WeRSoftware", then they could create their namespace and write it as:
namespace WeRSoftware {

  class Math
  {
   ...
  };
}
Each will create their own name space, and you may be able to create "Math" objects of each type.
int main()
{
   SoftwareOne::Math x;  // instance of SoftwareOne Math object
   WeRSoftware::Math y;  // instance of WeRSoftware Math object
}

std namespace

As far as you're concerned, you don't have to create namespaces. You simply have to use them. Most of the objects that you're familiar with (cout, cin, ostream, istream) all belong to the std namespace.

.h files

When referring to standard classes in .h files, you should preface them with the std namespace. Basically, you are referring to the classes by their "full name". That is, you will refer to them with the namespace and the class name.

For example, here is a class in a .h file.

#include <iostream>
#include <string>
#include <vector>

class Foo {
   std::string       name;
   std::vector<int>  scores;
public:
   void addScore( int num );
   void readin( std::istream &input );
   void print( std::ostream &output );
};
As you can see, each class type that is part of the standard library (string, istream, ostream, vector) is preceded by std and two colons.

Base types, such as int and float, are not classes, and so they are NOT preceded by std::.

.cpp files

While it's recommended that you give the fully qualified class names in the .h files (e.g. using std::string instead of string), you can use a shortcut in .cpp files.

You can "import" a namespace with the "using" directive. It looks like:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

void Foo::readin( istream &input )
{
  ....
}
Put the statement using namespace std; just after all the #include's. Admittedly, this is the "lazy" way to do it. Stanley Lippmann, who has written a rather authoritative book called C++ Primer, prefers explicitly importing each class, as in:
#include <iostream>
#include <string>
#include <vector>

 // import each class
using std::cout;
using std::cin;
using std::string;

void Foo::readin( istream &input )
{
  ....
}
The "using namespace std" directive basically says "whenever you see a class, check to see if it's in the std namespace first". Thus, when it sees the class, string, it checks to see if it's in the "std" namespace. Of course, it doesn't have to be (for example, Foo isn't in that namespace).

Summary

Here are the basic style guide for using namespaces.
  1. If you use a standard class in a .h file (e.g., istream, ostream, string), use the fully qualified name (e.g., std::istream, std::ostream, std::string). You do not use "std::" for classes you have written (e.g. Foo).
  2. If you are in a .cpp file, use
    using namespace std;
    
    and place it after the "#include" files.

A Note About #include

The C++ standard has changed the way #include files work. In particular, they no longer end in .h. For example, you now write:
#include <iostream>
instead of
#include <iostream.h>
Why this change? Partly, it has to do with namespaces. Partly, it has to do with there being no standard extension (some compilers expect a .H extension or a .hpp extension) for header files. So, the standard version picked something that was common to none of the vendors.

For library files that are commonly seen in C, you usually put a "c" in front of it. For example, instead of "stdio.h" and "stdlib.h", you write:

#include <cstdio>
#include <cstdlib>
This convention is still not quite universally used. So, if "cstdio" and "cstdlib" don't work, you may have to resort to the old-fashioned "stdio.h" and "stdlib.h".

g++ and checking of namespaces

The version of g++ used allows for namespaces, but doesn't seem to enforce it. In other words, if you use namespaces, it's happy and if you don't, it's happy. cxx, with the -std strict_ansi option checks to see if you are using namespaces correctly.

We'll use g++, but feel free to use cxx as a double-check, to make sure you are using the namespaces correctly. If we find the correct options that will cause namespaces to be handled correctly, we will inform you.