avtomaton
avtomaton

Reputation: 4874

Using functions from namespace inside other namespace

Is it any way to omit outer namespace name for some functions from other namespace inside top-level one?

void sample_func();

namespace foo {
void first_func();

namespace bar {
void second_func();
void sample_func();
}

Everything is trivial for first_func(): just typing using foo::first_func; allows to call it just as fist_func();

Everything is simple if I want to call second_func without any prefix: just using foo::bar::second_func; allows to call it as second_func();

But is there any way to call it as bar::second_func();? It will increase code readability - much better to type and see something like bar::sample_func instead of full foo::bar::sample_func without names confusion: obviously using namespace foo::bar is not an option in that case.

UPD I am not interested in importing the whole foo or bar namespace (i. e. using namespace ... directive! I need just some functions from them.

Upvotes: 1

Views: 1177

Answers (3)

n. m. could be an AI
n. m. could be an AI

Reputation: 119877

You can use

namespace bar = foo::bar;

to import foo::bar into current namespace as just bar.

Upvotes: 2

R Sahu
R Sahu

Reputation: 206577

You can use

using namespace foo;

in any declarative region where you wish to use just first_func() and bar::sample_func().

Example:

int main()
{
   using namespace foo;
   first_func();
   bar::sample_func();
}

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 59997

Prefix it with the namespace:: or :: if not in a namespace i.e

::sample_func();

foo::first_func();
bar::second_func();
bar::sample_func();

Upvotes: 0

Related Questions