C++ Namespace Alias

The other day, while I was reading up on Boost and TR1, I ran across an interesting feature called a namespace alias. With a namespace alias, you essentially call out a new namespace that will reference back to the one specified. The example where I had found this was in example code dealing with Boost::FileSystem:

#include "boost/filesystem.hpp"
namespace fs = boost::filesystem;
 
int main(int argc. char** argv)
{
   fs::path full_path("/tmp");
   ....
   return 0;
}

In this case, they created a namespace alias fs, referencing boost::filesystem. The only real reason I can see using this would be scenarios such as this. I’m not really a big fan of globally calling using namespace std, but using boost::filesystem at every library call may result in difficult to read code.  So, namespace aliasing could save me the extra (and precious) extra key presses required to use the full namespace.

This entry was posted in General, Software Development and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">