C ++ use characters from another namespace without making them accessible externally

Is there a construct similar to using namespace that does not make imported characters visible outside the body (or bodies) of the namespace?

In this example, each character in whatever and other_namespace will also be accessible through Foo::<name_of_symbol> ... and I would like to prevent this.

 namespace Foo { using namespace whatever; using namespace other_namespace; // some definitions } 

As a complete example, this program is valid. If there were an alternative using namespace with supposed semantics, it would not be.

 namespace a { int func(int x) { return 10; } } namespace b { using namespace a; } namespace c { int do_thing(int x) { return b::func(57); } } 
+5
source share
1 answer

You can use an alias inside an unnamed namespace.

 namespace a_long_namespace_name { void someFunc() {}; } namespace b { namespace { // an unnamed namespace namespace a = a_long_namespace_name; // create a short alias } void someOtherFunc() { a::someFunc(); } } b::a::someFunc(); // compiler error 

You still need to write a namespace to call the function, but this makes the calls shorter.

+5
source

Source: https://habr.com/ru/post/1270983/


All Articles