Namespaces, Includes, and the Standard Library

Namespace Aliases

What is a namespace alias and when would I use one?

Abstract art representing computer programming

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();
}
This Question is from the Lesson:

Namespaces, Includes, and the Standard Library

A quick introduction to namespaces in C++, alongside the standard library and how we can access it

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Namespaces, Includes, and the Standard Library

A quick introduction to namespaces in C++, alongside the standard library and how we can access it

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved