Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The namespace keyword

Reference for C++ namespace: defining and reopening namespaces, nesting, aliases, anonymous and inline namespaces, and using-directive pitfalls.

A namespace is a named scope for declarations. It groups related functions, types and variables and prevents their names from colliding with the same names in other libraries. The whole standard library lives in namespace std.

Namespaces can be reopened and extended in any number of places, nested (namespace a::b), given short aliases, and made anonymous so that their contents have internal linkage. An inline namespace makes its names visible as if they were in the parent, which is how libraries version their APIs.

C++
namespace geometry {
    double area(double r);
}
double a = geometry::area(2.0);

Try it

C++ Defining, nesting and aliasing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Anonymous and inline namespaces

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
namespace name { declarations }
namespace outer::inner { declarations }       // C++17
namespace { declarations }                    // anonymous
inline namespace name { declarations }        // C++11
namespace alias = existing::name;
using namespace name;                         // using-directive
using name::member;                           // using-declaration
Form Description
namespace name { } Defines or reopens name. A namespace can only be defined at namespace scope, not inside a function or class.
anonymous namespace Members have internal linkage and are visible in the rest of the translation unit without qualification. Replaces file-level static.
inline namespace Its members are treated as members of the enclosing namespace for lookup and specialization.
using namespace Makes names visible without qualification in the current scope.
using name::member Brings a single name into scope.

Usage notes

  • Never put using namespace std; in a header: every file that includes it silently inherits the directive and may suffer name clashes or changed overload resolution.
  • Even in source files prefer qualified names or specific using-declarations in small scopes.
  • The std namespace is reserved. You may not add declarations to it, except for specific template specializations for user-defined types (like std::hash).
  • ::name refers to the global namespace explicitly.
  • Argument-dependent lookup finds functions in the namespaces of their arguments' types, which is why swap(a, b) and operator<< work without qualification.
  • Anonymous namespaces in headers create a separate copy per translation unit; avoid them there.
  • An inline namespace lets you ship v1 and v2 side by side and select the default by moving the inline keyword; mangled names stay distinct for ABI safety.
  • A namespace alias is handy for long names (namespace fs = std::filesystem;).
  • Qualified lookup (ns::name) does not apply ADL; unqualified function calls do.