Variables, Scopes and Namespaces: Namespaces#

Adapted from: “Learn Modern C++” by cpptutor: Learn Modern C++: Variables, Scopes and Namespaces

Program that Demonstrates using Namespaces#

// 02-height.cpp : define the same variable name in two different namespaces
 
#include <print>
using namespace std;
 
namespace Wonderland {
    auto alice_height_m{ 0.15 };
}
 
namespace VictorianEngland {
    auto alice_height_m{ 0.9 };
}
 
int main() {
    println("Alice\'s height varies between {}m and {}m",
        Wonderland::alice_height_m,
        VictorianEngland::alice_height_m);
}

Explanation of the Above Code#

This C++ code demonstrates the use of namespaces to define variables with the same name in different contexts, avoiding naming conflicts.

Code Breakdown:#

#include <print>
using namespace std;
  • The <print> header is used for formatted output (similar to std::format in C++20). It allows the use of println for printing formatted strings.

  • using namespace std; allows direct access to standard library functions without prefixing them with std::.

namespace Wonderland {
    auto alice_height_m{ 0.15 };
}
  • A namespace Wonderland is defined.

  • Inside this namespace, a variable alice_height_m is declared and initialized with the value 0.15 (meters).

namespace VictorianEngland {
    auto alice_height_m{ 0.9 };
}
  • Another namespace VictorianEngland is defined.

  • Inside this namespace, a variable alice_height_m is declared and initialized with the value 0.9 (meters).

Purpose of Namespaces:#

  • Namespaces allow variables with the same name (alice_height_m) to coexist without conflict because they are scoped to their respective namespaces (Wonderland and VictorianEngland).

int main() {
    println("Alice's height varies between {}m and {}m",
        Wonderland::alice_height_m,
        VictorianEngland::alice_height_m);
}
  • The main function prints a formatted string using the println function.

  • The variables Wonderland::alice_height_m and VictorianEngland::alice_height_m are accessed using the namespace resolution operator (::).

  • The output will display the values of alice_height_m from both namespaces.

Output:#

The program will produce the following output:

Alice's height varies between 0.15m and 0.9m

Key Concepts:#

  1. Namespaces: Namespaces are used to organize code and prevent naming conflicts by grouping related variables, functions, or classes.

  2. Namespace Resolution Operator (::): This operator is used to access members of a specific namespace.

  3. Variable Scoping: The same variable name can exist in different namespaces without conflict.

This program demonstrates how namespaces can be used to manage naming conflicts in C++ effectively.

Compile and Run Code#

Use Python to Change to Working Directory#

import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Cpp_Code/02_Variables_Scopes_and_Namespaces"
os.chdir(code_dir)

Use Docker to Compile the Code in a C++23 Environment#

!docker run --rm -v $(pwd):/app cpp23-clang18:latest clang++-18 -std=c++23 -stdlib=libc++ /app/02-height.cpp -o /app/02-height

Use Docker to Run Executable in a C++23 Environment#

!docker run --rm -v $(pwd):/app cpp23-clang18:latest ./02-height
Alice's height varies between 0.15m and 0.9m