Namespace Aliases
What is a namespace alias and when would I use one?
A namespace alias is a way to give a shorter or more convenient name to a namespace. You create a namespace alias using the namespace keyword followed by the alias name and an = sign.
For example:
#include <iostream>
namespace VeryLongNamespace {
void Foo() {
std::cout << "VeryLongNamespace::Foo()\n";
}
}
namespace VLN = VeryLongNamespace;
int main() {
VLN::Foo();
}VeryLongNamespace::Foo()Here, VLN is an alias for VeryLongNamespace. We can use VLN::Foo() instead of VeryLongNamespace::Foo().
You would use a namespace alias:
- To make your code more readable by shortening long namespace names.
- To avoid typing out long namespace names repeatedly.
- To create a shorter name for a nested namespace.
For example:
namespace A::B::C {
void Foo() {}
}
namespace ABC = A::B::C;
int main() {
ABC::Foo();
}Namespaces, Includes, and the Standard Library
A quick introduction to namespaces in C++, alongside the standard library and how we can access it