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

Viewed 68574

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

5 Answers

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;

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

Related