In C++, what is a "namespace alias"?

C++Namespaces

C++ Problem Overview


What is a "namespace alias" in C++? How is it used?

C++ Solutions


Solution 1 - C++

A namespace alias is a convenient way of referring to a long namespace name by a different, shorter name.

As an example, say you wanted to use the numeric vectors from Boost's uBLAS without a using namespace directive. Stating the full namespace every time is cumbersome:

boost::numeric::ublas::vector<double> v;

Instead, you can define an alias for boost::numeric::ublas -- say we want to abbreviate this to just ublas:

namespace ublas = boost::numeric::ublas;


ublas::vector<double> v;

Solution 2 - C++

Quite simply, the #define won't work.

namespace Mine { class MyClass { public: int i; }; }
namespace His = Mine;
namespace Yours { class Mine: public His::MyClass { void f() { i = 1; } }; }

Compiles fine. Lets you work around namespace/class name collisions.

namespace Nope { class Oops { public: int j; }; }
#define Hmm Nope
namespace Drat { class Nope: public Hmm::Oops { void f () { j = 1; } }; }

On the last line, "Hmm:Oops" is a compile error. The pre-processor changes it to Nope::Oops, but Nope is already a class name.

Solution 3 - C++

More on this topic http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Core-C-/Stephan-T-Lavavej-Core-C-1-of-n

It is all about choosing an alias for a looong namespace name, such as:

namespace SHORT = NamespaceFirst::NameSpaceNested::Meow

Then later, you can typedef

typedef SHORT::mytype

instead of

typedef NamespaceFirst::NameSpaceNested::Meow::mytype

This syntax only works for namespaces, cannot include classes, types after the namespace NAME =

Solution 4 - C++

Also note that namespace aliases and using directives are resolved at compile time, not run time. (More specifically, they're both tools used to tell the compiler where else to look when resolving names, if it can't find a particular symbol in the current scope or any of its parent scopes.) For example, neither of these will compile:

namespace A {
    int foo;
    namespace AA {
        int bar;
    } // namespace AA
    namespace AB {
        int bar;
    } // namespace AB
} // namespace A
namespace B {
    int foo;
    namespace BA {
        int bar;
    } // namespace BA
    namespace BB {
        int bar;
    } // namespace BB
} // namespace B

bool nsChooser1, nsChooser2;
// ...

// This doesn't work.
namespace C = (nsChooser1 ? A : B);
C::foo = 3;

// Neither does this.
// (Nor would it be advisable even if it does work, as compound if-else blocks without braces are easy to inadvertently break.)
if (nsChooser1)
    if (nsChooser2)
        using namespace A::AA;
    else
        using namespace A::AB;
else
    if (nsChooser2)
        using namespace B::BA;
    else
        using namespace B::BB;

Now, a curious mind may have noticed that constexpr variables are also used at compile time, and wonder whether they can be used in conjunction with either an alias or a directive. To my knowledge, they cannot, although I may be wrong about this. If you need to work with identically-named variables in different namespaces, and choose between them dynamically, you would have to use references or pointers.

// Using the above namespaces...
int& foo = (nsChooser1 ? A::foo : B::foo);

int* bar;
if (nsChooser1) {
    if (nsChooser2) {
        bar = &A::AA::bar;
    } else {
        bar = &A::AB::bar;
    }
} else {
    if (nsChooser2) {
        bar = &B::BA::bar;
    } else {
        bar = &B::BB::bar;
    }
}

The usefulness of the above may be limited, but it should serve the purpose.

(My apologies for any typoes I may have missed in the above.)

Solution 5 - C++

Namespace is used to prevent name conflicts.

For example:

namespace foo {
    class bar {
        //define it
    };
}
 
namespace baz {
    class bar {
        // define it
    };
}

You now have two classes name bar, that are completely different and separate thanks to the namespacing.

The "using namespace" you show is so that you don't have to specify the namespace to use classes within that namespace. ie std::string becomes string.

my resource: https://www.quora.com/What-is-namespace-in-C++-1

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartin BView Question on Stackoverflow
Solution 1 - C++Martin BView Answer on Stackoverflow
Solution 2 - C++user2168377View Answer on Stackoverflow
Solution 3 - C++kiriloffView Answer on Stackoverflow
Solution 4 - C++Justin Time - Reinstate MonicaView Answer on Stackoverflow
Solution 5 - C++maiView Answer on Stackoverflow