using "multiple"namespaces one-liner

132 Views Asked by At

is there a simplified way to include more namespaces instead of typing every time the same things. This is very annoying, especially in the .h files.

For instance:

Instead of writing:

int f() {
    using namespace blabla1;
    using namespace blabla2;
    using namespace blabla3;

}

I would prefer:

using myNamespace = blabla1, blabla2, blabla3;

int f() {
    using namespace myNamespace;
    /// this will be equivalent to the previous example
    }

Thanks

2

There are 2 best solutions below

0
On BEST ANSWER

Using directives are transitive. So if you aggregate them into a single namespace

namespace All {
    using namespace A;
    using namespace B;
    using namespace C;
}

You can then simply do

using namespace All;

And unqualified name lookup will work.

Live example

4
On

I'm not sure if this helps you, but if you want to avoid multiple using statements each time, you can wrap the above namespaces, into another namespace:

namespace myNameSpace {
  using namespace blabla1;
  using namespace blabla2;
  using namespace blabla3;
}

and then use it like this:

int f() {
    using namespace myNameSpace;
}

Here's a demo.