Namespace alias in the class definition is illegal, as specified by the language specification.
Its allowed in only in namespace scope or function scope.
You can make an alias at the namespace scope. But this will create a permanent alias which can be used from other files as well. However, the solution is simple: you can use an unnamed namespace to prevent the alias (and therefore all symbols from the big namespace) from being visible from other files. This is how it can be done:
//MyFile.cpp
namespace myspace
{
namespace //this is unnamed namespace
{
namespace abc = a_big_namespace;
}
struct MyClass
{
void fn()
{
abc::test(); //don't worry, this will work!
}
};
}
//OtherFile.cpp
myspace::abc::test(); //error - that means, prevention worked.
The alias is not visible from other files. When compiling OtherFile.cpp
, GCC (4.5.0) says,
‘myspace::abc’ has not been declared
That proves the alias abc
is visible only in MyFile.cpp
. Thanks to the unnamed namespace.
Demo: http://www.ideone.com/2zyNI (though it doesn’t demonstrate OtherFile concept since I cannot have more than one file at ideone.com)