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

Nested namespaces

Use case

Cleaner namespace declarations.

Explanation

Nested namespaces can be declared with :: separator in a single line.

Code

#include <cstdint>
#include <iostream>

// C++17:
namespace re::macho::arm64 {
// $ echo "nop" | llvm-mc -triple=aarch64 -show-encoding
// nop                                     // encoding: [0x1f,0x20,0x03,0xd5]
constexpr uint32_t NOP = 0xD503201F;
} // namespace re::macho::arm64

// Before C++17:
// namespace re { namespace { macho { namespace arm64 {...}}}}

int main() {
  std::cout << "NOP: 0x" << std::hex << re::macho::arm64::NOP << "\n";
  return 0;
}

View on GitHub.

Output

$ ./src/c++17/build/nested-namespaces
NOP: 0xd503201f