3

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

veltres
  • 51
  • 5

2 Answers2

6

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

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
3

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.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • Same solution at the same time, damn, I don't know how do people deal with these cases. Both of you deserve the best answer – veltres May 13 '20 at 13:57
  • 1
    Thanks :) Note that you can only accept one answer, but you can upvote multiple answers if you find them useful. – cigien May 13 '20 at 13:58
  • I have a new account, so I need some points to upvote. I will definitely upvote both – veltres May 13 '20 at 14:01
  • ok upvoted both. I am going to put the best answer to StoryTeller only because he explained the that using directives are transitive. Thank you again mate – veltres May 13 '20 at 14:32