Namespaces allow different parts of a program to reuse the same short name safely.

name conflict Two namespaces can each contain a function with the same name, and qualified names choose between them.

Avoiding Name Conflicts

value
avoiding_name_conflicts.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <string>

namespace text_view {
std::string format(int value) {
    return "text:" + std::to_string(value);
}
}

namespace math_view {
std::string format(int value) {
    return "double:" + std::to_string(value * 2);
}
}

int main() {
    int value = 6;

    std::string text = text_view::format(value);
    std::string math = math_view::format(value);

    std::cout << text << std::endl;
    std::cout << math << std::endl;
    return 0;
}
#include <iostream>
#include <string>

namespace text_view {
std::string format(int value) {
    return "text:" + std::to_string(value);
}
}

namespace math_view {
std::string format(int value) {
    return "double:" + std::to_string(value * 2);
}
}

int main() {
    int value = 2;

    std::string text = text_view::format(value);
    std::string math = math_view::format(value);

    std::cout << text << std::endl;
    std::cout << math << std::endl;
    return 0;
}
#include <iostream>
#include <string>

namespace text_view {
std::string format(int value) {
    return "text:" + std::to_string(value);
}
}

namespace math_view {
std::string format(int value) {
    return "double:" + std::to_string(value * 2);
}
}

int main() {
    int value = 9;

    std::string text = text_view::format(value);
    std::string math = math_view::format(value);

    std::cout << text << std::endl;
    std::cout << math << std::endl;
    return 0;
}
  1. value ← 6

    16int main() {17    int value→ 6 = 6; //@value=2, 91819    std::string text = text_view::format(value6);20    std::string math = math_view::format(value);
  2. std::string format(int value)

    4namespace text_view {5std::string format(int value6) {6    return "text:" + std::to_string(value6);7}
  3. text ← text:6

    19std::string text→ text:6 = text_view::format(value6);20std::string math = math_view::format(value6);
  4. std::string format(int value)

    10namespace math_view {11std::string format(int value6) {12    return "double:" + std::to_string(value6 * 2);13}
  5. math ← double:12

    19    std::string text = text_view::format(value);20    std::string math→ double:12 = math_view::format(value6);2122    std::cout << texttext:6 << std::endl;23    std::cout << mathdouble:12 << std::endl;24    return 0;25}
    outputtext:6
    double:12
  1. value ← 2

    16int main() {17    int value→ 2 = 2;1819    std::string text = text_view::format(value2);20    std::string math = math_view::format(value);
  2. std::string format(int value)

    4namespace text_view {5std::string format(int value2) {6    return "text:" + std::to_string(value2);7}
  3. text ← text:2

    19std::string text→ text:2 = text_view::format(value2);20std::string math = math_view::format(value2);
  4. std::string format(int value)

    10namespace math_view {11std::string format(int value2) {12    return "double:" + std::to_string(value2 * 2);13}
  5. math ← double:4

    19    std::string text = text_view::format(value);20    std::string math→ double:4 = math_view::format(value2);2122    std::cout << texttext:2 << std::endl;23    std::cout << mathdouble:4 << std::endl;24    return 0;25}
    outputtext:2
    double:4
  1. value ← 9

    16int main() {17    int value→ 9 = 9;1819    std::string text = text_view::format(value9);20    std::string math = math_view::format(value);
  2. std::string format(int value)

    4namespace text_view {5std::string format(int value9) {6    return "text:" + std::to_string(value9);7}
  3. text ← text:9

    19std::string text→ text:9 = text_view::format(value9);20std::string math = math_view::format(value9);
  4. std::string format(int value)

    10namespace math_view {11std::string format(int value9) {12    return "double:" + std::to_string(value9 * 2);13}
  5. math ← double:18

    19    std::string text = text_view::format(value);20    std::string math→ double:18 = math_view::format(value9);2122    std::cout << texttext:9 << std::endl;23    std::cout << mathdouble:18 << std::endl;24    return 0;25}
    outputtext:9
    double:18