liveBook · Manning

Namespace

As explained in 100 C++ Mistakes and How to Avoid Them

The global namespace is the C++ namespace containing declarations that are not enclosed in another namespace.

The global namespace contains declarations not enclosed in another namespace. Placing unrelated enumeration constants there creates collisions, weakens names, and lets integer values hide which meaning a variable carries. section 4.2

Mistake: global namespace enums

TrafficColor and PaintColor place every enumerator in one namespace. Names such as Yellow can collide, while int color = 2 loses whether the value means a traffic color or paint color. Because the enums behave as integers, arbitrary or out-of-range values can also pass through unnoticed. section 4.2

Listing 4.3 Problematic global namespace enumerations
enum TrafficColor {
    Green,
    Yellow,                                                #1
    Red                                                    #2
};
 
enum PaintColor {
    Gray,
    White,                                                 #3
    Black,
    Paint_Red,                                             #4
    Blue,
    Paint_Yellow,
    Paint_Green
};
 
int main() {
    int color = 2;                                         #5
    switch (color) {
    case Red:
        std::cout << "we have a red one\n";
        break;
    case Blue:
        std::cout << "we have a blue one\n";
        break;
    case Yellow:
        std::cout << "we have a yellow one\n";
        break;
    default:
        std::cout << "we have a white one\n";
        break;
    // case White:                                         #6
    };
    return 0;
}

Inspect the problematic enumeration design, including duplicate color meanings, unqualified case labels, and an uninformative integer assignment. section 4.2

Prefer enum class to give each enumeration its own scope. Qualified values such as Traffic::Yellow and Paint::Blue preserve intent and prevent unrelated color sets from mixing accidentally. section 4.2

Listing 4.4 Separate namespace enumerations
enum class Traffic {                                     #1
    Green,
    Yellow,
    Red
};
 
enum class Paint {                                       #2
    Gray,
    Black,
    White,
    Red,
    Blue,
    Yellow,
    Green
};
 
int main() {
    auto color = Traffic::Yellow;                        #3
    switch (color) {
    case Traffic::Red:                                   #4
        std::cout << "we have a red light\n";
        break;
    case Traffic::Yellow:
        std::cout << "we have a yellow light\n";
        break;
    case Traffic::Green:
    default:
        std::cout << "we have a green light\n";
        break;
    };
 
    Paint can = Paint::Blue;
    switch (can) {
    case Paint::Red:
        std::cout << "we have a red cube\n";
        break;
    case Paint::Yellow:
        std::cout << "we have a blue cube\n";
        break;
    case Paint::White:
        [[fallthrough]];    
    default:                                             #5
        std::cout << "we have a white cube\n";
        break;
    };
    return 0;
}

Compare separately scoped Traffic and Paint enumerations with qualified switch cases and type-specific variables. section 4.2

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Defines the global namespace and traces duplicate names, integer mixing, arbitrary values, and out-of-bounds assignments before recommending enum class.
Examples
Uses traffic-light and automobile paint colors to contrast unscoped enumerations with separately scoped enum classes.
What you can do after
Apply it via enum class, qualified enumerators, and separate types for unrelated color sets.
How it connects
Connects the global namespace to enumerations, enum class, namespaces, switch statements, and backing integers.

Also explained in

Compare how 20 books explain namespace →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.