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:

  1. To make your code more readable by shortening long namespace names.
  2. To avoid typing out long namespace names repeatedly.
  3. 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

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Why Use Namespaces in C++?
What are the benefits of using namespaces in C++? When should I use them in my code?
Multiple using Statements
Can I have multiple using statements for the same namespace in different scopes? What happens if I do?
Setting Include Paths
How do I set up my compiler to find header files in different directories?
Header Include Order
Does the order of #include directives matter? If so, what's the correct order?
Explicit std:: Namespace
Why do you use explicit std:: namespace qualifiers in this lesson's code examples instead of 'using namespace std;'?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant