Item 23. Namespaces

Global scope was getting overly crowded. Everybody and his brother implemented libraries that reused the same names for different classes and functions. For example, many libraries wanted to include a class named String, but if you used two different libraries that defined a String type, you’d get a multiple definition error or worse. Various extra-language approaches used to address this problem (naming conventions, the preprocessor, ...) only made things worse. Namespaces to the rescue.

In some ways, namespaces introduce complexity (see Argument Dependent Lookup [25, 89]), but most uses of namespaces are simple and simplifying. A namespace is a subdivision of global scope:

This code snippet opens a namespace called

Get C++ Common Knowledge: Essential Intermediate Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.