Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Inline namespaces

Use case

API versioning with default version.

Explanation

inline namespace makes its contents accessible from the parent namespace. Analyzer::run() calls V2::run() because V2 is marked inline. Old code can still explicitly use V1.

Code

#include <cstdint>
#include <iostream>

namespace Analyzer {
namespace V1 {
void run() { std::cout << "V1\n"; }
} // namespace V1

// Default.
inline namespace V2 {
void run() { std::cout << "V2\n"; }
} // namespace V2
} // namespace Analyzer

int main() {
  Analyzer::run();
  Analyzer::V1::run();
  Analyzer::V2::run();
  return 0;
}

View on GitHub.

Output

$ ./src/c++11/build/inline-namespaces
V2
V1
V2