Namespace aliases in C++

Posted: 9th September 2010 by Tim in C++
Tags: , , , , ,

In C++, it’s good practice to explicitly specify the namespace of a class instead of using the using syntax. This makes your code more readable, more explicit and is generally just good style. Sometimes these namespaces can get unconveniently long. This is where namespace aliases come in.

Imagine you have a person class in the my::really::long::namespace::to::my::project namespace. You don’t want to write this out every time. Instead, you can add the following alias:

namespace myproj = my::really::long::namespace::to::my::project;

This class may now be referred to like so:

myproj::person

If used correctly, such aliases can make the code easier to read and manage. Just make sure the alias name is a logical nickname.