Namespace alias inside a namespace

Viewed 132

I have a following structure of namespaces

namespace a::b::c
{
   class xyz{

   };

   using x=xyz
}
  1. If I want to refer to x in an outside file, How should I address it? is it like a::b::c::x or just x?

  2. To be able to refer to it as x outside my code, how can I change the above code? Can I write using x=a::b::xyz inside the namespace a::b::c?

2 Answers

The name x is introduced in the namespace a::b::c

namespace a::b::c
{
   class xyz{

   };

   using x=xyz
}

So in the global namespace to refer to the name x you have to write

a::b::c::x

You could introduce the alias in the global namespace like

using x = a::b::c::xyz;

Alternatively you could use a using declaration like

using a::b::c::x;

in this case you can use the unqualified name x in the scope where the using declaration is placed.

Okay, you need to understand that namespace means a new name space in order to distinguish between possibly similar names.

When you use: namespace a::b::c you add to all of the names inside it the prefix a::b::c, therefore, you must call it using a::b::c::x as you mentioned (and you can easily check it on your own!). Only inside of that namespace, you can use it as x.

You can use it in other places by including it into the namespace, by putting using a::b::c::x, but I don't understand why you want to do such a thing.

If you want to use it everywhere, just put the using x = a::b::c::x outside of the namespace.

But, namespaces are created with a good purpose, please avoid removing the namespace prefix by doing any of these, it is just a bad habit.

Related