I have a lot of namespace usage in an initialiser list and would like a using namespace to reduce the verbosity. However the initialiser list is outside the scope of the constructor braces so I would have to place the using outside the constructor and pollute the rest of the file with it. Is there a way to scope the using as I want? Rather than:
MyClass::MyClass() :
m_one(nsConstants::ONE),
m_two(nsConstants::TWO),
m_three(nsConstants::THREE)
{}
I want:
MyClass::MyClass() :
using namespace nsConstants;
m_one(ONE),
m_two(TWO),
m_three(THREE)
{}
_
You can’t. The standard offers some less good alternatives:
Now, from least polluting to most polluting.
typedefmakes it possible to write that alias in aprivatesection of your class definition:But you can also use it unit-globally, but with an opportunity to rename it:
You can also alias namespaces:
Or you can cherry pick symbols from other namespaces, with the disadvantage that your
Frobmay collide with anotherFrobin your unit of translation:Just for the sake of completeness, the most polluting solution is
using namespace.Note that III, IV and V can also be limited to your cxx-file, like in the Schwarzschild-example.