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;

- 23,670
- 6
- 53
- 72
-
8To possibly explain the downvotes, SO is not and never will be a replacement forv a good C++ textbook. The question you posed will be answered by any such book. And the SO "feature" of answering your own questions should not be used to provide paraphrases of such books. – Jul 31 '09 at 09:02
-
31No offense taken... Just to explain why I did this: It was my understanding from Joel's comments in the podcast that even "entry-level" questions were fair game on SO, and that it was acceptable to ask a question and answer it yourself if that content wasn't on SO yet in an accessible form. But apparently, this is frowned upon? – Martin B Jul 31 '09 at 09:17
-
2There is certainly an etiquette to answering your own question, to avoid irritation; in this case, it is pretty obvious that it never **was** a real question. For example, http://stackoverflow.com/questions/494927/stack-overflow-etiquette-for-answering-your-own-question – Marc Gravell Jul 31 '09 at 09:29
-
6@Martin B: I don't agree that this is an entry level question - in fact there have been far more obvious questions asked in the past now with many votes. Having said that, people might feel you're simply trying to gain reputation for yourself. A way round this is to mark one or both of the question/answer as "community wiki". Personally I'd go with question as you and answer as community. If the question has merrit then you'll get ROI. – Richard Corden Jul 31 '09 at 09:35
-
The answer in Marc's link (ie. putting your answer into the question) is a good alternative too. – Richard Corden Jul 31 '09 at 09:37
-
Thanks -- didn't intend to offend or siphon off reputation. For what it's worth, at least I learned something, and the answer is in community wiki now. – Martin B Jul 31 '09 at 09:39
-
1My recollection is that Spolsky specficically encouraged this type of question & self-answer (even if the answer was known when the question was posed). However, a quick search has left this as only a recollection. In any case, the community has apparently decided otherwise. – Michael Burr Jul 31 '09 at 15:55
-
2I think the important question is whether the question is real" -- is it something that you have been asked? Is it something that people want to know? Is it something that hasn't already been asked and answered on SO? If you read the SO blog post about the R community posting and answering questions here, note that they picked the top X questions which their community *actually kept asking*, so it had real-world relevance. Taking random snippets of language-specific knowledge and posting them here seems less useful. – jalf Jul 31 '09 at 19:44
-
but when that is said, a significant part of the community simply sees red when they see someone answering his own question. I don't think it's such a bad thing (again, the R thing was a great idea), but *some* people think it's cheating. – jalf Jul 31 '09 at 19:45
-
A problem with this question might be that you'd only ever find it if you already knew the answer. If you don't know what a namespace alias is, you're not likely to find this question in the first place. – jalf Jul 31 '09 at 19:47
-
9I've been programming in C++ for several years, my textbook is an ocean away (literally), and I don't remember the syntax of namespace aliases. Thanks to this question, the answer was a click away. So independently of the author answering their own question, it is a good question, and a good answer. Thanks :) – WaterFox Dec 03 '20 at 05:38
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.

- 89
- 1
- 2
-
3What #define? Perhaps your answer refers to a previous version of the question? – einpoklum Feb 29 '16 at 23:27
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 =

- 25,609
- 37
- 148
- 229
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.)

- 4,149
- 24
- 40
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

- 5
- 2
-
However it is not recommended to use "using namespace" in your code. It can generate confusion if a file with `using namespace` will be included in different file of the application. Suddenly different file starts to use this namespace as well. Then a developer may not know from which library the function has been used. (he thought it was from std but it was some other namespace in fact) – Adam Kuzański Jul 22 '22 at 07:59