Friday, February 15, 2008

c++: namespace alias

Recently found interesting feature in c++: alias to namespace.

namespace A
{
    int A = 5;
};

namespace B = A;
I wonder what is it for ... ?

3 comments:

Cheba said...

One more power feature to be misused?

Ni@m said...

Not actually.
You may rename namespace if you have namespace with a very long name:
namespace verylongnameofthenamecpace
{
}
namespace A = verylongnameofthenamecpace;
or simplify usage of nested:
namespace A
{
namespace A
{
namespace A
{
}
}
}
namespace B = A::A::A;
Also some rare problems are faced here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1526.txt

Bob said...

As Ni@m mentioned, it's useful when dealing with namespaces with long names, or that are deeply nested.

You'll appreciate namespace aliasing when you spend some time with the boost libraries.

I've written a bit more about it's utility here: http://www.juicydata.com/CppNamespaceAlias.