Is it Okay to Implement Using Directives in C++?

In C++ using directives such as “using namespace std” are derided by most experienced programmers as dangerous and something to be avoided. This can sometimes be confusing when people provide little explanation as to what a using directive actually is. Often it ends up that all the knowledge you walk away with is that using directives make it so you don’t have to write “std::” before certain parts of the code but it’s dangerous to use them. To add to the confusion a lot of beginner programming books will actually recommend their readers implement “using namespace std” to save time, which only serves to make people unsure of what the best practice is.

What Actually Is A Using Directive?

The easiest way to explain a using directive is by breaking it down into each of its three components. Firstly there is the using keyword which just tells the compiler that you are going to be using something.

After adding the using keyword to the code you need to tell the compiler what it is that your using, in this case its a namespace. This is followed by what the namespace that you’re using is called such as “std” for example.

The final product of this is that compiler is being alerted that you are using the std namespace in all of the code that is within the scope of this using declaration.

OK Well What’s a Namespace and What Does It Mean to Be “Using” One?

A namespace is a declared region in a program that is given its own special name to prevent the code within this region from colliding with other code that might have the same name. For example if you were making a program you might create the following function:

void foo(){
// code here
}

Later on though you may want to create a different function with the same name such as:

int foo(){
// code here
}

If you try to call the foo() function the compiler will have no idea which foo you’re trying to call. It will either throw an exception or even worse, just pick one of them to use at random which can have some very undesired effects if it picks the wrong one. The way to fix this is by putting each function into its own namespace like the following:

namespace one{
	int foo(){
	// code here
	}
}
namespace two{
	void foo(){
	// code here
	}
}

Now if you want to call one of the foo functions you can easily differentiate between the two like this:

one::foo();
two::foo();

If you tried to call a foo function without a namespace in front of it now, the compiler will not recognize it.

one::foo(); // Works just fine
foo(); //Will now cause an error

If you tell the compiler you’re “using namespace one” for example it, whenever you use something from the one namespace you will no longer have to prefix it with “one::”. This is because the compiler is now just assuming that whenever you put in “foo” for instance, you mean the foo from one and not any other foo.

Why Is This Considered Dangerous?

A using directive completely defeats the purpose of having the namespace in the first place, which is to prevent collisions between pieces of code. If you look back to my foo example from earlier you’ll quickly realise that if you were to put in both “using namespace one” and “using namespace two” the program would completely break. Either the compiler will get confused and throw an exception or pick a foo function at random, just like what would happen if the namespaces weren’t there.

For smaller projects this isn’t much of an issue, which is why some programming tutorials wrongly tell newer programmers to insert “using namespace std” at the top of their code. Chances are if you’re a beginner you aren’t using any namespace other than std so all this is doing is saving some time with no immediate risk . However as you get better at programming you’ll be making progressively larger and larger programs and it’ll get to the point where you will be working with other namespaces, either ones you’ve declared or ones from external libraries that you’ve imported. When you do make a large program with more namespaces you definitely do not want to be in the habit of implementing using directives as now it definitely can break something.

Is There A Time When Using Directive’s Aren’t Dangerous?

Using declarations are only as dangerous as I said earlier when you just stick them at the very top of a file and not contained inside any class or function. This is because when you do this you have now declared your using this namespace at the global level, meaning everything in the whole file is now using the namespace which of course causes the problems mentioned earlier.

However if you use a namespace inside a function or class, that using directive will only effect code inside that specific function or class and not anything outside of it. This can be useful in a few specific instances in higher level coding with classes but doesn’t come up at the low to mid levels and is still fairly rare at those higher levels.

Final Thoughts

A lot of people like to say “using directives are dangerous” and this is definitely true as I’ve shown above, but not wholly accurate. Better advice on the usage of using directives would be “using directives at the global scope are dangerous but they can be helpful at the local scope.” In general using directives should be avoided but they are occasionally helpful in specific circumstances.

If you have any comments or questions email me at nick@crumbsofcode.com