From 80005e1e9bab8066f02b54dd87c79a4787e303d5 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Sat, 8 Apr 2017 15:10:32 -0300 Subject: [PATCH] Add section about inline namespaces As suggested on issue #15, inline namespaces were missing from the documentation, so I added a sentence with a snippet :) --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index dbc8f8e..c1a4b90 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ C++11 includes the following new language features: - [special member functions for move semantics](#special-member-functions-for-move-semantics) - [converting constructors](#converting-constructors) - [explicit conversion functions](#explicit-conversion-functions) +- [inline-namespaces](#inline-namespaces) C++11 includes the following new library features: - [std::move](#stdmove) @@ -995,6 +996,24 @@ B b{}; if (b); // OK calls B::operator bool() bool bb = b; // error copy-initialization does not consider B::operator bool() ``` +### Inline namespaces +All members of an inline namespace are treated as if they were part of its parent namespace, allowing specialization of functions and easing the process of versioning. This is a transitive property, if A contains B, which in turn contains C and both B and C are inline namespaces, C's members can be used as if they were on A. + +```c++ +namespace Program { + namespace Version1 { + int getVersion() { return 1; } + bool isFirstVersion() { return true; } + } + inline namespace Version2 { + int getVersion() { return 2; } + } +} + +int version {Program::getVersion()}; // Uses getVersion() from Version2 +int oldVersion {Program::Version1::getVersion()}; // Uses getVersion() from Version1 +bool firstVersion {Program::isFirstVersion()}; // Does not compile when Version2 is added +``` ## C++11 Library Features