Nested namespace definition (revision 2) (original) (raw)
Document number: | N4230 |
---|---|
Date: | 2014-10-10 |
Project: | Programming Language C++, Evolution Working Group |
Reply-to: | Robert Kawulak , Andrew Tomazos |
I. Table of Contents
- II. Revision History
- III. Introduction
- IV. Motivation
- V. Impact On the Standard
- VI. Design Decisions
- VII. Technical Specifications
II. Revision History
Note: this document (revision 2) is based on N4026 (the initial proposal) and partially incorporates N4116 (revision 1), which was not based on the original document.
Changes from N4026:
- Reworded Open Issues into Design Decisions following the feedback from Rapperswil.
- Incorporated and modified wording for Core Feature from N4116.
- Added wording for Usage in the Standard.
III. Introduction
The paper proposes allowing the use of a qualified name in a namespace definition to define several nested namespaces at once, for example:
namespace A::B::C { //… }
The code above would be equivalent to:
namespace A { namespace B { namespace C { //… } } }
The feature was already proposed by Jon Jagger in 2003 in the paper N1524 Nested Namespace Definition Proposal, but it has been listed in N2869 State of C++ Evolution (Post San Francisco 2008) under Not ready for C++0x, but open to resubmit in future.
IV. Motivation
Programmers' demand
There is clearly a need for a more concise way of defining nested namespaces than what the language offers today. The feature is asked for over and over by C++ users; see for example a few of the top web search results for a related query:
- ISO C++ Standard – Future Proposals forum: nested namespace declaration
- ISO C++ Standard – Future Proposals forum: Namespaces-related open questions
- Stack Overflow: Is there a better way to express nested namespaces in C++ within the header
- Stack Overflow: Is there a specific reason nested namespace declarations are not allowed in C++?
- Stack Overflow: Multiple namespace declaration in C++
- Visual Studio UserVoice: Nested namespace declarations (Note that it is currently the 20th top voted idea out of 238 for C++.) Quite possibly some of the users asking for this feature are novices/newcomers from other languages who used this syntax intuitively and found that their compiler doesn't accept it. This might indicate that the new syntax would not only mean saving some typing to experienced programmers, but it would also mean one confusion less to C++ learners.
Prevalence
It is not uncommon to find many deeply nested namespaces in large projects. For example, a search using the regular expression pattern (namespace\s+\w+\s*\{\s*){3,}
to find nested namespace definitions at least 3 levels deep in the include directory of Boost libraries yielded 3758 matches and the greatest nesting level found this way was 7 (4 matches).
Existing practice in other languages
An analogous syntax is found and heavily used in the C# programming language. Some other languages do not even allow for nesting of namespace definitions but only for providing a hierarchical namespace specification in a single declaration (e.g. Java or PHP).
Existing practice in C++
The author is not aware of any C++ compiler supporting this feature; however Lzz, a tool that automates many onerous C++ programming tasks, supports it – from the tool's documentation:
The name of a named namespace may be qualified.
namespace A::B { typedef int I; }
is equivalent to:
namespace A { namespace B { typedef int I; } }
V. Impact On the Standard
The proposal describes a pure language extension which is a non-breaking change – code that was previously ill-formed now would have well-defined semantics.
VI. Design Decisions
This section describes some issues related to the proposed feature and corresponding decisions based on the guidance from the EWG given at the Rapperswil meeting.
Attributes
Currently, the C++ Standard doesn't allow for specification of attributes for namespaces, but a resolution to the EWG issue #113 may lift this limitation. This proposal does not take attributes into account, but support for attributes can be added by a later proposal if found desirable.
Inline namespaces
It is not clear what is the best way to deal with inline namespaces. One possibility is that only the innermost namespace is defined inline, another one is that all the specified namespaces are inline. In either case the semantics seem to be potentially confusing, thus this proposal does not allow for inline qualified namespace definitions at all.
Namespace aliases
By analogy, qualified names could also be allowed in namespace alias definitions, e.g.
using namespace A::B::C = X;
However, use cases for qualified namespace aliases seem to be infrequent (the author didn't find any in Boost for example) so they aren't considered by this proposal.
VII. Technical Specifications
The proposed wording defines the core feature and makes use of it throughout the Standard text where applicable. It is relative to the N3797 working draft.
Core Feature
- Change § 7.3.1 p. 1 [namespace.def] and § A.6 [gram.dcl] as follows:
…
namespace-name:
original-namespace-name
namespace-alias
original-namespace-name:
identifier
namespace-definition:
named-namespace-definition
unnamed-namespace-definition
nested-namespace-definition
named-namespace-definition:
original-namespace-definition
extension-namespace-definition
original-namespace-definition:
inline
optnamespace
identifier{
namespace-body}
extension-namespace-definition:
inline
optnamespace
original-namespace-name{
namespace-body}
unnamed-namespace-definition:
inline
optnamespace
{
namespace-body}
nested-namespace-definition:
namespace
enclosing-namespace-specifier::
identifier{
namespace-body}
enclosing-namespace-specifier:
identifier
enclosing-namespace-specifier::
identifier
namespace-body:
declaration-seqopt
… - Add new paragraph 10 to § 7.3.1 [namespace.def]:
A nested-namespace-definition with an enclosing-namespace-specifier
E
, identifierI
and namespace-bodyB
is equivalent tonamespace E { namespace I { B } }
[Example:
```
namespace A::B::C {
int i;
}The above has the same effect as:
namespace A { namespace B { namespace C { int i; } } }
_—end example_]
Usage in the Standard
- Change § 20.9.9.1.4 [func.bind.place] as follows:
```
namespace std::placeholders {
namespace placeholders {
…
}
}
- Change § 28.5.1 [re.synopt] as follows:
```
namespace std::regex_constants {
namespace regex_constants {
…
}
}
- Change § 28.5.2 [re.matchflag] as follows:
```
namespace std::regex_constants {
namespace regex_constants {
…
}
}
- Change § 28.5.3 [re.err] as follows:
```
namespace std::regex_constants {
namespace regex_constants {
…
}
}
- Change § 30.3.2 [thread.thread.this] as follows:
```
namespace std::this_thread {
namespace this_thread {
…
}
}
```