mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-17 18:14:36 +03:00
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 :)
This commit is contained in:
19
README.md
19
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user