Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| c_namespaces [2018/02/01 10:44] – [Composition and selection] rpjday | c_namespaces [2018/02/01 14:50] (current) – [using-Directives] rpjday | ||
|---|---|---|---|
| Line 41: | Line 41: | ||
| namespace ATT = American_Telephone_and_Telegraph; | namespace ATT = American_Telephone_and_Telegraph; | ||
| </ | </ | ||
| + | |||
| + | Also, namespace identifier is "... either a previously unused identifier, in which case this is // | ||
| ===== Extensibility (namespaces are " | ===== Extensibility (namespaces are " | ||
| Line 70: | Line 72: | ||
| ==== using-Declarations ==== | ==== using-Declarations ==== | ||
| + | |||
| + | Read [[http:// | ||
| "makes the symbol name from the namespace ns_name accessible for unqualified lookup as if declared in the same class scope, block scope, or namespace as where this using-declaration appears." | "makes the symbol name from the namespace ns_name accessible for unqualified lookup as if declared in the same class scope, block scope, or namespace as where this using-declaration appears." | ||
| Line 91: | Line 95: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | Useful for introducing base class members into derived class definitions. | ||
| ==== using-Directives ==== | ==== using-Directives ==== | ||
| + | |||
| + | cppreference.com link [[http:// | ||
| "From the point of view of unqualified name lookup of any name after a using-directive and until the end of the scope in which it appears, every name from ns_name is visible as if it were declared in the nearest enclosing namespace which contains both the using-directive and ns_name." | "From the point of view of unqualified name lookup of any name after a using-directive and until the end of the scope in which it appears, every name from ns_name is visible as if it were declared in the nearest enclosing namespace which contains both the using-directive and ns_name." | ||
| Line 157: | Line 165: | ||
| ===== Nested namespaces ===== | ===== Nested namespaces ===== | ||
| - | Coming soon ... | + | < |
| + | void h(); | ||
| + | |||
| + | namespace X { | ||
| + | void g(); | ||
| + | // ... | ||
| + | namespace Y { | ||
| + | void f(); | ||
| + | void ff(); | ||
| + | // ... | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void X:: | ||
| + | { | ||
| + | f(); g(); h(); // all fine | ||
| + | } | ||
| + | |||
| + | void X::g() | ||
| + | { | ||
| + | f(); // error: no f() in X | ||
| + | Y:: | ||
| + | } | ||
| + | |||
| + | void h() | ||
| + | { | ||
| + | f(); // error: no global f() | ||
| + | Y:: | ||
| + | X:: | ||
| + | X:: | ||
| + | } | ||
| + | </ | ||
| + | NOTE: C++17 allows '' | ||
| ===== Namespace aliases ===== | ===== Namespace aliases ===== | ||
| Line 177: | Line 217: | ||
| </ | </ | ||
| - | A namespace alias cannot be used to re-open a namespace. | + | * Aliases can be used to refer to nested namespaces. |
| + | * A namespace alias cannot be used to re-open a namespace. | ||
| - | (Add ability to alias nested namespaces.) | ||
| ===== Inline namespaces (new in C++11) ===== | ===== Inline namespaces (new in C++11) ===== | ||
| Line 185: | Line 225: | ||
| Supports versioning: | Supports versioning: | ||
| + | |||
| + | < | ||
| + | namespace Lib | ||
| + | inline namespace V2_0 { | ||
| + | ... V2.0 declarations ... | ||
| + | } | ||
| + | | ||
| + | namespace V1_0 { | ||
| + | ... V1.0 declarations ... | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Or create your own namespace based on that feature: | ||
| < | < | ||
| Line 218: | Line 272: | ||
| </ | </ | ||
| + | One last weird variation, where you can choose your own default: | ||
| + | |||
| + | < | ||
| + | namespace MyDefault { | ||
| + | inline | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | "I do not recommend such intricate use of header files unless it is really necessary. The example above repeatedly violates the rules against including into a nonlocal scope and against having a syntactic construct span file boundaries (the use of inline); see §15.2.2. Sadly, I have seen worse." | ||
| ===== Unnamed namespaces ===== | ===== Unnamed namespaces ===== | ||
| - | Its members have potential scope from their point of declaration to the end of the translation unit, and have internal linkage. | + | Its members have potential scope from their point of declaration to the end of the translation unit, and have //internal// linkage. The aim is to preserve locality of code rather than to present an interface to users. |
| + | |||
| + | < | ||
| + | #include " | ||
| + | namespace { | ||
| + | int a; | ||
| + | void f() {...} | ||
| + | int g() | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | An unnamed namespace has an implied using-directive. | ||
| + | " | ||
| ===== Argument dependent lookup (ADL) ===== | ===== Argument dependent lookup (ADL) ===== | ||