Home > Enterprise >  Namespaces and C library
Namespaces and C library

Time:10-11

Is a namespace contained in a library or a library is contained in a namespace? How many namespaces are there in the C standard library ?

CodePudding user response:

They are orthogonal. A library can use multiple namespaces and a namespace can be split between multiple libraries. However it is a good practice to scope the contents of a library to a (usually single) namespace ( namespaces nested therein) specific to that library to avoid name clash between multiple libraries and for clarity.

The standard library uses (aside from the global namespace scope) only the std namespace ( nested namespaces), although namespaces named std followed by any number of digits and the namespace posix are also reserved to the standard library for future standardization. The standard library also has multiple nested namespaces inside std, e.g. std::filesystem and std::ranges, and certain names are reserved for it in the global namespace scope.

The standard library also explicitly allows user code (including user libraries) to add certain declarations (in particular partial specializations of some classes) to the std namespace. So std is not always completely restricted to the standard library either.

  • Related