5.2.4 · D1C++ Programming

Foundations — Namespaces — avoiding name collisions

2,159 words10 min readBack to topic

This page assumes nothing. Before you can understand the parent topic, you need a small pile of vocabulary. We build each word from a picture, in an order where every idea rests on the one before it.


0. The picture we keep returning to

Think of your whole program as a set of nested boxes. The biggest box is everything (the "global" world). Inside it you can draw smaller boxes. A name written on a slip of paper lives in exactly one box, but you can reach it from outside by naming the boxes on the way in.

Figure — Namespaces — avoiding name collisions

Hold this image. Every symbol below is a label, a wall, or a path in this box picture.


1. Identifier — a name you invent

An identifier by itself is just a short word. It carries no address yet — that comes next.


2. Scope — the box a name lives in

We already have a whole vault note on this — read Scope and Storage Duration if the idea feels shaky. For us, the key move is: a namespace is one particular kind of scope — a named one.


3. Global scope — the outermost box


4. The scope-resolution operator ::

Now we can define the symbol the parent uses on almost every line.

Figure — Namespaces — avoiding name collisions

Special case — leading :: with nothing on the left. Writing ::name means "start at the very top, the global box." This matters when a local name has hidden the global one and you want to reach past it. Cover this case now so it never surprises you later.


5. Fully-qualified name — the complete address


6. Collision — when two full names are identical


7. Name lookup — how the compiler finds an unqualified name

Figure — Namespaces — avoiding name collisions

8. using — making a name visible without its box

The parent uses two flavours. Define both from the same picture.


9. Translation unit & linkage — why "same name, two files" can still break

The parent's ODR mistakes need two more words.


10. Where std fits


The prerequisite map

Identifier - a name

Scope - box a name lives in

Global scope - outer box

Namespace - a named box

Scope operator ::

Fully qualified name

Collision - equal full names

Namespaces topic

Name lookup

using declaration and directive

Translation unit

Linkage internal vs external

One Definition Rule

std namespace

Each arrow means "you must understand the source before the target makes sense." Notice every path ends at the topic — nothing here is optional.


Equipment checklist

Test yourself. Cover the answers; you should be able to say each aloud before reading the parent note.

What is an identifier, in the box picture?
The word written on a slip of paper — a name you invented for a variable, function, class, or type.
What does "scope" mean physically?
The walls of one box — the region of code where a name is in effect and visible.
What is the global scope?
The outermost box: the region not inside any function, class, or named namespace.
Read Geometry::area in plain words.
"Look inside the box Geometry for the slip named area."
What does a leading :: (as in ::swap) mean?
Start the search at the very top — the global box — ignoring any nearer name that hides it.
What is a fully-qualified name?
The complete address: every box label from outer to inner, joined by ::, ending in the identifier.
State the collision test in words.
Two different things collide exactly when their full names are identical; different prefixes mean no collision.
Name the two ways name lookup can fail.
No match anywhere (undeclared) — or two equally-good matches at one level (ambiguous).
Difference between using-declaration and using-directive?
Declaration pins in ONE name's short form; directive makes ALL of a box's short names findable.
Does using change a name's true address or grant privacy?
No — it only affects which short names lookup will notice; the real full address is untouched.
What is a translation unit?
One .cpp file plus everything it includes, compiled as a single chunk.
Internal vs external linkage in one line each?
Internal = name private to its own file; external = name shared across files (so a duplicate definition clashes).
Why can Ns::x defined in two files break the build?
A namespace is open across files, so it becomes one externally-linked entity defined twice — an ODR violation.

Connections

  • 5.2.04 Namespaces — avoiding name collisions (Hinglish) (the parent topic)
  • Scope and Storage Duration
  • Header Files and Include Guards
  • Linkage — internal vs external
  • One Definition Rule (ODR)
  • Classes and Encapsulation
  • The Standard Library (std)