Namespaces in C++

Last time we talked about namespaces in general, and I gave an introduction to what they are, and why we need them. This time, lets look at how to implement namespaces in C++.

Implementing namespaces in C++ is very simple, all you need to do is use the namespace keyword plus an identifier that will be the name of your namespace:


namespace my_namespace {
  //code here
  int my_var = 1;
}

Inside the curly braces ({,}) you put your code. Everything you declare in there, such as functions, variables, and classes will be “inside” the namespace my_namespace. To access entities inside an identifier we use the :: scope operator:


my_namespace::my_var;

You can even add namespaces inside namespaces:


namespace my_namespace {
  //code here
  int my_var = 1;
  namespace internal {
  }
}

That is all there is to creating namespaces.

You should always try to use a unique indentifier for your namespace so that it won’t conflict with another namespace.

Using “using”

In C++ there are using declarations and using directives. They both do different things, and you may want to use one or the other depending on what you are trying to accomplish.

In my C++ study group, the professor has recommended to place the following directive on every program:

using namespace std;

If you’ve been reading the tutorials I recommended a couple of posts back, you will see that they also use the same directive. However, the first time I saw that, something told me there was something wrong with that. Sure, if you are building simple 10 line hello world programs you won’t have any problem, but if you plan on actually writing large, complex programs, you may want to reconsider adding that directive at the top of every source file you write.

I won’t go into details on why using that line of code can be potentially a bad idea because for now, I think it is safe to use it, but keep in mind that when you start writing actual programs, you may want to use the full qualified name of your identifiers.

I mentioned there are two kinds of using expressions: using directives, and using declarations. Using directives “import” everything within a namespace into a namespaces that is shared by the namespace we are “importing” and the current scope, while using declarations introduce a name directly into the current scope. Lets look at how both of these statements look:


// using directive
using namespace std;
// using declaration
using std::std

A simple way to remember which one is a directive and which one is a declaration is to think of a using declaration as a statement that is declaring a name into the current scope. Or maybe just remember that if it has the namespace keyword it is most likely a using directive.

So, what is the actual difference between both of them? The difference is hard to understand if you haven’t fully understood the concept of scope, plus you need to understand another bit of how languages work.

Name Resolution

When you do something like


#include 
using namespace std;
int two = 2;
int main() {
  int four = two + two;
  cout << four;
  return 0;
}

The following is happening:

When main is called, it sees that you are trying to add the value of two to itself, and return the result to be assigned into a variable four. This starts a lookup process which looks inside the function for a variable called “two”. It does not find it, then since there is no other scope, such as a class, or a namespace, it goes up to the global scope, and finds two in there. It gets the value of the variable, adds it to itself, and returns the result to be stored in the variable four.

This lookup process happens every time you use an identifier. Lookup will look for the entity identified with the name provided in all of the scopes shared by the current scope until it finds it. In other words, it will go up the scope chain until it finds what it is looking for. Once it finds the first occurrence of the name, it uses that entity, and lookup ends. Why does this matter anyway?

The importance of this process becomes obvious when you try to understand the difference between using directives and using declarations, but we will leave that for another time.

Here are some documents you may want to read next:

http://www.devx.com/tips/Tip/12534 – Using Declarations and using Directives
http://www.cplusplus.com/doc/tutorial/namespaces/#namespace – The section on namespaces in one of the tutorials I’m following.
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – A closer look at why “using namespace std;” Might not be a good idea.