Namespaces in C++

Jinku Hu Oct 12, 2023
  1. Use the namespace Keyword to Create a New Namespace in C++
  2. Use Nested Namespaces to Implement Complex Scopes in C++
Namespaces in C++

This article will explain how to use namespaces in C++.

Use the namespace Keyword to Create a New Namespace in C++

C++ has the concept of namespaces that help prevent a common issue in large projects called name conflicts. When the given project uses different libraries developed by independent teams and has many global names for different objects, it becomes inevitable that some names will match and cause errors. Namespace declares a scope where certain names of functions or other objects are defined. Remember that there are automatic scope rules in C++ that govern how the names of objects are seen and available in different code regions. E.g., the local variable declared in the function is not seen or accessible outside the function body. So, another variable with the same name can be declared outside of this function’s body without any conflicts. On the other, we a single global scope, which is often utilized by large programs to make certain objects available across the files or functions. Now, imagine how the space without any manual scoping mechanism can be managed.

In fact, programmers used to deal with name collision using very long names for global objects. It can work for relatively small-sized projects, but it makes the code quite messy to read, and the solution still can not prevent name collisions without a coordinated naming scheme.
Namespaces provide a mechanism to partition a global scope (consequently called namespace). A namespace is declared with the namespace keyword followed by the namespace name itself. Then follows the code in curly braces similar to the function block, and it ends without a semicolon. The same automatic scope rules govern variables within a namespace as other parts of the code. The object names that need to be accessed from outside of the namespace need to start with the namespace name in which they are defined, followed by double colon notation and then the object name. The following code example demonstrates the basic use case where for the namespaces in the same file.

#include <iostream>

using std::cout;
using std::endl;

namespace dinno {
int var1 = 456;
}

int main() {
  int var1 = 123;

  cout << var1 << endl;
  cout << dinno::var1 << endl;

  return EXIT_SUCCESS;
}

Output:

123
456

Use Nested Namespaces to Implement Complex Scopes in C++

Namespaces can be nested, similar to the loop statements. By default, a nested namespace has access to the object names in the outer namespace, but the latter (often called a parent namespace) can not access the members in the inner namespace. Although, one can specify the keyword inline before the inner namespace declaration to make its members accessible in the parent namespace. Namespaces can be defined in different places. Namely, the same namespace definition can span different files or separate places in the single file as dinno namespaces are defined in the following example.

#include <iostream>

using std::cout;
using std::endl;

namespace dinno {
int var1 = 456;
}

namespace gini {
int var1 = 980;

namespace bean {
int var1 = 199;
}
}  // namespace gini

namespace dinno {
int var2 = 990;
}

int var1 = 123;

int main() {
  cout << var1 << endl;
  cout << dinno::var1 << endl;
  cout << gini::var1 << endl;
  cout << dinno::var2 << endl;
  cout << gini::bean::var1 << endl;

  return EXIT_SUCCESS;
}

Output:

123
456
980
990
199
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook