From Jason Turner

[basic.namespace]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp0olw666f/{from.md → to.md} +100 -551
tmp/tmp0olw666f/{from.md → to.md} RENAMED
@@ -4,10 +4,27 @@ A namespace is an optionally-named declarative region. The name of a
4
  namespace can be used to access entities declared in that namespace;
5
  that is, the members of the namespace. Unlike other declarative regions,
6
  the definition of a namespace can be split over several parts of one or
7
  more translation units.
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  The outermost declarative region of a translation unit is a namespace;
10
  see  [[basic.scope.namespace]].
11
 
12
  ### Namespace definition <a id="namespace.def">[[namespace.def]]</a>
13
 
@@ -24,46 +41,46 @@ namespace-definition:
24
  nested-namespace-definition
25
  ```
26
 
27
  ``` bnf
28
  named-namespace-definition:
29
- 'inline'ₒₚₜ 'namespace' attribute-specifier-seqₒₚₜ identifier '{' namespace-body '}'
30
  ```
31
 
32
  ``` bnf
33
  unnamed-namespace-definition:
34
- 'inline'ₒₚₜ 'namespace' attribute-specifier-seqₒₚₜ '{' namespace-body '}'
35
  ```
36
 
37
  ``` bnf
38
  nested-namespace-definition:
39
- 'namespace' enclosing-namespace-specifier '::' identifier '{' namespace-body '}'
40
  ```
41
 
42
  ``` bnf
43
  enclosing-namespace-specifier:
44
  identifier
45
- enclosing-namespace-specifier '::' identifier
46
  ```
47
 
48
  ``` bnf
49
  namespace-body:
50
  declaration-seqₒₚₜ
51
  ```
52
 
53
- Every *namespace-definition* shall appear in the global scope or in a
54
- namespace scope ([[basic.scope.namespace]]).
55
 
56
  In a *named-namespace-definition*, the *identifier* is the name of the
57
- namespace. If the *identifier*, when looked up (
58
- [[basic.lookup.unqual]]), refers to a *namespace-name* (but not a
59
- *namespace-alias*) that was introduced in the namespace in which the
60
- *named-namespace-definition* appears or that was introduced in a member
61
- of the inline namespace set of that namespace, the
62
- *namespace-definition* *extends* the previously-declared namespace.
63
- Otherwise, the *identifier* is introduced as a *namespace-name* into the
64
- declarative region in which the *named-namespace-definition* appears.
65
 
66
  Because a *namespace-definition* contains *declaration*s in its
67
  *namespace-body* and a *namespace-definition* is itself a *declaration*,
68
  it follows that *namespace-definition*s can be nested.
69
 
@@ -117,22 +134,22 @@ The optional *attribute-specifier-seq* in a *named-namespace-definition*
117
  appertains to the namespace being defined or extended.
118
 
119
  Members of an inline namespace can be used in most respects as though
120
  they were members of the enclosing namespace. Specifically, the inline
121
  namespace and its enclosing namespace are both added to the set of
122
- associated namespaces used in argument-dependent lookup (
123
- [[basic.lookup.argdep]]) whenever one of them is, and a
124
- *using-directive* ([[namespace.udir]]) that names the inline namespace
125
- is implicitly inserted into the enclosing namespace as for an unnamed
126
- namespace ([[namespace.unnamed]]). Furthermore, each member of the
127
- inline namespace can subsequently be partially specialized (
128
- [[temp.class.spec]]), explicitly instantiated ([[temp.explicit]]), or
129
- explicitly specialized ([[temp.expl.spec]]) as though it were a member
130
- of the enclosing namespace. Finally, looking up a name in the enclosing
131
- namespace via explicit qualification ([[namespace.qual]]) will include
132
- members of the inline namespace brought in by the *using-directive* even
133
- if there are declarations of that name in the enclosing namespace.
134
 
135
  These properties are transitive: if a namespace `N` contains an inline
136
  namespace `M`, which in turn contains an inline namespace `O`, then the
137
  members of `O` can be used as though they were members of `M` or `N`.
138
  The *inline namespace set* of `N` is the transitive closure of all
@@ -143,26 +160,29 @@ namespaces.
143
 
144
  A *nested-namespace-definition* with an *enclosing-namespace-specifier*
145
  `E`, *identifier* `I` and *namespace-body* `B` is equivalent to
146
 
147
  ``` cpp
148
- namespace E { namespace I { B } }
149
  ```
150
 
 
 
 
151
  [*Example 3*:
152
 
153
  ``` cpp
154
- namespace A::B::C {
155
  int i;
156
  }
157
  ```
158
 
159
  The above has the same effect as:
160
 
161
  ``` cpp
162
  namespace A {
163
- namespace B {
164
  namespace C {
165
  int i;
166
  }
167
  }
168
  }
@@ -173,21 +193,21 @@ namespace A {
173
  #### Unnamed namespaces <a id="namespace.unnamed">[[namespace.unnamed]]</a>
174
 
175
  An *unnamed-namespace-definition* behaves as if it were replaced by
176
 
177
  ``` bnf
178
- 'inline'ₒₚₜ 'namespace' 'unique ' '{ /* empty body */ }'
179
- 'using namespace' 'unique ' ';'
180
- 'namespace' 'unique ' '{' namespace-body '}'
181
  ```
182
 
183
  where `inline` appears if and only if it appears in the
184
- *unnamed-namespace-definition* and all occurrences of `unique ` in a
185
  translation unit are replaced by the same identifier, and this
186
  identifier differs from all other identifiers in the translation unit.
187
  The optional *attribute-specifier-seq* in the
188
- *unnamed-namespace-definition* appertains to `unique `.
189
 
190
  [*Example 1*:
191
 
192
  ``` cpp
193
  namespace { int i; } // unique::i
@@ -212,19 +232,19 @@ void h() {
212
  — *end example*]
213
 
214
  #### Namespace member definitions <a id="namespace.memdef">[[namespace.memdef]]</a>
215
 
216
  A declaration in a namespace `N` (excluding declarations in nested
217
- scopes) whose *declarator-id* is an *unqualified-id* ([[dcl.meaning]]),
218
- whose *class-head-name* (Clause [[class]]) or *enum-head-name* (
219
- [[dcl.enum]]) is an *identifier*, or whose *elaborated-type-specifier*
220
- is of the form *class-key* *attribute-specifier-seq*ₒₚₜ *identifier* (
221
- [[dcl.type.elab]]), or that is an *opaque-enum-declaration*, declares
222
- (or redeclares) its *unqualified-id* or *identifier* as a member of `N`.
223
 
224
- [*Note 1*: An explicit instantiation ([[temp.explicit]]) or explicit
225
- specialization ([[temp.expl.spec]]) of a template does not introduce a
226
  name and thus may be declared using an *unqualified-id* in a member of
227
  the enclosing namespace set, if the primary template is declared in an
228
  inline namespace. — *end note*]
229
 
230
  [*Example 1*:
@@ -242,14 +262,14 @@ namespace X {
242
  ```
243
 
244
  — *end example*]
245
 
246
  Members of a named namespace can also be defined outside that namespace
247
- by explicit qualification ([[namespace.qual]]) of the name being
248
- defined, provided that the entity being defined was already declared in
249
- the namespace and the definition appears after the point of declaration
250
- in a namespace that encloses the declaration’s namespace.
251
 
252
  [*Example 2*:
253
 
254
  ``` cpp
255
  namespace Q {
@@ -268,32 +288,32 @@ namespace R {
268
  }
269
  ```
270
 
271
  — *end example*]
272
 
273
- If a `friend` declaration in a non-local class first declares a class,
274
- function, class template or function template[^5] the friend is a member
275
- of the innermost enclosing namespace. The `friend` declaration does not
276
- by itself make the name visible to unqualified lookup (
277
- [[basic.lookup.unqual]]) or qualified lookup ([[basic.lookup.qual]]).
278
 
279
  [*Note 2*: The name of the friend will be visible in its namespace if a
280
  matching declaration is provided at namespace scope (either before or
281
  after the class definition granting friendship). — *end note*]
282
 
283
  If a friend function or function template is called, its name may be
284
  found by the name lookup that considers functions from namespaces and
285
- classes associated with the types of the function arguments (
286
- [[basic.lookup.argdep]]). If the name in a `friend` declaration is
287
- neither qualified nor a *template-id* and the declaration is a function
288
- or an *elaborated-type-specifier*, the lookup to determine whether the
289
- entity has been previously declared shall not consider any scopes
290
- outside the innermost enclosing namespace.
291
 
292
- [*Note 3*: The other forms of `friend` declarations cannot declare a
293
- new member of the innermost enclosing namespace and thus follow the
294
- usual lookup rules. — *end note*]
295
 
296
  [*Example 3*:
297
 
298
  ``` cpp
299
  // Assume f and g have not yet been declared.
@@ -339,11 +359,11 @@ namespace-alias:
339
  identifier
340
  ```
341
 
342
  ``` bnf
343
  namespace-alias-definition:
344
- 'namespace' identifier '=' qualified-namespace-specifier ';'
345
  ```
346
 
347
  ``` bnf
348
  qualified-namespace-specifier:
349
  nested-name-specifierₒₚₜ namespace-name
@@ -372,485 +392,15 @@ namespace CWVLN = Company_with_very_long_name; // OK: duplicate
372
  namespace CWVLN = CWVLN;
373
  ```
374
 
375
  — *end example*]
376
 
377
- ### The `using` declaration <a id="namespace.udecl">[[namespace.udecl]]</a>
378
-
379
- ``` bnf
380
- using-declaration:
381
- 'using' using-declarator-list ';'
382
- ```
383
-
384
- ``` bnf
385
- using-declarator-list:
386
- using-declarator '...'ₒₚₜ
387
- using-declarator-list ',' using-declarator '...'ₒₚₜ
388
- ```
389
-
390
- ``` bnf
391
- using-declarator:
392
- 'typename'ₒₚₜ nested-name-specifier unqualified-id
393
- ```
394
-
395
- Each *using-declarator* in a *using-declaration* [^6] introduces a set
396
- of declarations into the declarative region in which the
397
- *using-declaration* appears. The set of declarations introduced by the
398
- *using-declarator* is found by performing qualified name lookup (
399
- [[basic.lookup.qual]], [[class.member.lookup]]) for the name in the
400
- *using-declarator*, excluding functions that are hidden as described
401
- below. If the *using-declarator* does not name a constructor, the
402
- *unqualified-id* is declared in the declarative region in which the
403
- *using-declaration* appears as a synonym for each declaration introduced
404
- by the *using-declarator*.
405
-
406
- [*Note 1*: Only the specified name is so declared; specifying an
407
- enumeration name in a *using-declaration* does not declare its
408
- enumerators in the *using-declaration*'s declarative
409
- region. — *end note*]
410
-
411
- If the *using-declarator* names a constructor, it declares that the
412
- class *inherits* the set of constructor declarations introduced by the
413
- *using-declarator* from the nominated base class.
414
-
415
- Every *using-declaration* is a *declaration* and a *member-declaration*
416
- and can therefore be used in a class definition.
417
-
418
- [*Example 1*:
419
-
420
- ``` cpp
421
- struct B {
422
- void f(char);
423
- void g(char);
424
- enum E { e };
425
- union { int x; };
426
- };
427
-
428
- struct D : B {
429
- using B::f;
430
- void f(int) { f('c'); } // calls B::f(char)
431
- void g(int) { g('c'); } // recursively calls D::g(int)
432
- };
433
- ```
434
-
435
- — *end example*]
436
-
437
- In a *using-declaration* used as a *member-declaration*, each
438
- *using-declarator*'s *nested-name-specifier* shall name a base class of
439
- the class being defined. If a *using-declarator* names a constructor,
440
- its *nested-name-specifier* shall name a direct base class of the class
441
- being defined.
442
-
443
- [*Example 2*:
444
-
445
- ``` cpp
446
- template <typename... bases>
447
- struct X : bases... {
448
- using bases::g...;
449
- };
450
-
451
- X<B, D> x; // OK: B::g and D::g introduced
452
- ```
453
-
454
- — *end example*]
455
-
456
- [*Example 3*:
457
-
458
- ``` cpp
459
- class C {
460
- int g();
461
- };
462
-
463
- class D2 : public B {
464
- using B::f; // OK: B is a base of D2
465
- using B::e; // OK: e is an enumerator of base B
466
- using B::x; // OK: x is a union member of base B
467
- using C::g; // error: C isn't a base of D2
468
- };
469
- ```
470
-
471
- — *end example*]
472
-
473
- [*Note 2*: Since destructors do not have names, a *using-declaration*
474
- cannot refer to a destructor for a base class. Since specializations of
475
- member templates for conversion functions are not found by name lookup,
476
- they are not considered when a *using-declaration* specifies a
477
- conversion function ([[temp.mem]]). — *end note*]
478
-
479
- If a constructor or assignment operator brought from a base class into a
480
- derived class has the signature of a copy/move constructor or assignment
481
- operator for the derived class ([[class.copy]]), the
482
- *using-declaration* does not by itself suppress the implicit declaration
483
- of the derived class member; the member from the base class is hidden or
484
- overridden by the implicitly-declared copy/move constructor or
485
- assignment operator of the derived class, as described below.
486
-
487
- A *using-declaration* shall not name a *template-id*.
488
-
489
- [*Example 4*:
490
-
491
- ``` cpp
492
- struct A {
493
- template <class T> void f(T);
494
- template <class T> struct X { };
495
- };
496
- struct B : A {
497
- using A::f<double>; // ill-formed
498
- using A::X<int>; // ill-formed
499
- };
500
- ```
501
-
502
- — *end example*]
503
-
504
- A *using-declaration* shall not name a namespace.
505
-
506
- A *using-declaration* shall not name a scoped enumerator.
507
-
508
- A *using-declaration* that names a class member shall be a
509
- *member-declaration*.
510
-
511
- [*Example 5*:
512
-
513
- ``` cpp
514
- struct X {
515
- int i;
516
- static int s;
517
- };
518
-
519
- void f() {
520
- using X::i; // error: X::i is a class member and this is not a member declaration.
521
- using X::s; // error: X::s is a class member and this is not a member declaration.
522
- }
523
- ```
524
-
525
- — *end example*]
526
-
527
- Members declared by a *using-declaration* can be referred to by explicit
528
- qualification just like other member names ([[namespace.qual]]).
529
-
530
- [*Example 6*:
531
-
532
- ``` cpp
533
- void f();
534
-
535
- namespace A {
536
- void g();
537
- }
538
-
539
- namespace X {
540
- using ::f; // global f
541
- using A::g; // A's g
542
- }
543
-
544
- void h()
545
- {
546
- X::f(); // calls ::f
547
- X::g(); // calls A::g
548
- }
549
- ```
550
-
551
- — *end example*]
552
-
553
- A *using-declaration* is a *declaration* and can therefore be used
554
- repeatedly where (and only where) multiple declarations are allowed.
555
-
556
- [*Example 7*:
557
-
558
- ``` cpp
559
- namespace A {
560
- int i;
561
- }
562
-
563
- namespace A1 {
564
- using A::i, A::i; // OK: double declaration
565
- }
566
-
567
- struct B {
568
- int i;
569
- };
570
-
571
- struct X : B {
572
- using B::i, B::i; // error: double member declaration
573
- };
574
- ```
575
-
576
- — *end example*]
577
-
578
- [*Note 3*: For a *using-declaration* whose *nested-name-specifier*
579
- names a namespace, members added to the namespace after the
580
- *using-declaration* are not in the set of introduced declarations, so
581
- they are not considered when a use of the name is made. Thus, additional
582
- overloads added after the *using-declaration* are ignored, but default
583
- function arguments ([[dcl.fct.default]]), default template arguments (
584
- [[temp.param]]), and template specializations ([[temp.class.spec]],
585
- [[temp.expl.spec]]) are considered. — *end note*]
586
-
587
- [*Example 8*:
588
-
589
- ``` cpp
590
- namespace A {
591
- void f(int);
592
- }
593
-
594
- using A::f; // f is a synonym for A::f; that is, for A::f(int).
595
- namespace A {
596
- void f(char);
597
- }
598
-
599
- void foo() {
600
- f('a'); // calls f(int), even though f(char) exists.
601
- }
602
-
603
- void bar() {
604
- using A::f; // f is a synonym for A::f; that is, for A::f(int) and A::f(char).
605
- f('a'); // calls f(char)
606
- }
607
- ```
608
-
609
- — *end example*]
610
-
611
- [*Note 4*: Partial specializations of class templates are found by
612
- looking up the primary class template and then considering all partial
613
- specializations of that template. If a *using-declaration* names a class
614
- template, partial specializations introduced after the
615
- *using-declaration* are effectively visible because the primary template
616
- is visible ([[temp.class.spec]]). — *end note*]
617
-
618
- Since a *using-declaration* is a declaration, the restrictions on
619
- declarations of the same name in the same declarative region (
620
- [[basic.scope]]) also apply to *using-declaration*s.
621
-
622
- [*Example 9*:
623
-
624
- ``` cpp
625
- namespace A {
626
- int x;
627
- }
628
-
629
- namespace B {
630
- int i;
631
- struct g { };
632
- struct x { };
633
- void f(int);
634
- void f(double);
635
- void g(char); // OK: hides struct g
636
- }
637
-
638
- void func() {
639
- int i;
640
- using B::i; // error: i declared twice
641
- void f(char);
642
- using B::f; // OK: each f is a function
643
- f(3.5); // calls B::f(double)
644
- using B::g;
645
- g('a'); // calls B::g(char)
646
- struct g g1; // g1 has class type B::g
647
- using B::x;
648
- using A::x; // OK: hides struct B::x
649
- x = 99; // assigns to A::x
650
- struct x x1; // x1 has class type B::x
651
- }
652
- ```
653
-
654
- — *end example*]
655
-
656
- If a function declaration in namespace scope or block scope has the same
657
- name and the same parameter-type-list ([[dcl.fct]]) as a function
658
- introduced by a *using-declaration*, and the declarations do not declare
659
- the same function, the program is ill-formed. If a function template
660
- declaration in namespace scope has the same name, parameter-type-list,
661
- return type, and template parameter list as a function template
662
- introduced by a *using-declaration*, the program is ill-formed.
663
-
664
- [*Note 5*:
665
-
666
- Two *using-declaration*s may introduce functions with the same name and
667
- the same parameter-type-list. If, for a call to an unqualified function
668
- name, function overload resolution selects the functions introduced by
669
- such *using-declaration*s, the function call is ill-formed.
670
-
671
- [*Example 10*:
672
-
673
- ``` cpp
674
- namespace B {
675
- void f(int);
676
- void f(double);
677
- }
678
- namespace C {
679
- void f(int);
680
- void f(double);
681
- void f(char);
682
- }
683
-
684
- void h() {
685
- using B::f; // B::f(int) and B::f(double)
686
- using C::f; // C::f(int), C::f(double), and C::f(char)
687
- f('h'); // calls C::f(char)
688
- f(1); // error: ambiguous: B::f(int) or C::f(int)?
689
- void f(int); // error: f(int) conflicts with C::f(int) and B::f(int)
690
- }
691
- ```
692
-
693
- — *end example*]
694
-
695
- — *end note*]
696
-
697
- When a *using-declarator* brings declarations from a base class into a
698
- derived class, member functions and member function templates in the
699
- derived class override and/or hide member functions and member function
700
- templates with the same name, parameter-type-list ([[dcl.fct]]),
701
- cv-qualification, and *ref-qualifier* (if any) in a base class (rather
702
- than conflicting). Such hidden or overridden declarations are excluded
703
- from the set of declarations introduced by the *using-declarator*.
704
-
705
- [*Example 11*:
706
-
707
- ``` cpp
708
- struct B {
709
- virtual void f(int);
710
- virtual void f(char);
711
- void g(int);
712
- void h(int);
713
- };
714
-
715
- struct D : B {
716
- using B::f;
717
- void f(int); // OK: D::f(int) overrides B::f(int);
718
-
719
- using B::g;
720
- void g(char); // OK
721
-
722
- using B::h;
723
- void h(int); // OK: D::h(int) hides B::h(int)
724
- };
725
-
726
- void k(D* p)
727
- {
728
- p->f(1); // calls D::f(int)
729
- p->f('a'); // calls B::f(char)
730
- p->g(1); // calls B::g(int)
731
- p->g('a'); // calls D::g(char)
732
- }
733
-
734
- struct B1 {
735
- B1(int);
736
- };
737
-
738
- struct B2 {
739
- B2(int);
740
- };
741
-
742
- struct D1 : B1, B2 {
743
- using B1::B1;
744
- using B2::B2;
745
- };
746
- D1 d1(0); // ill-formed: ambiguous
747
-
748
- struct D2 : B1, B2 {
749
- using B1::B1;
750
- using B2::B2;
751
- D2(int); // OK: D2::D2(int) hides B1::B1(int) and B2::B2(int)
752
- };
753
- D2 d2(0); // calls D2::D2(int)
754
- ```
755
-
756
- — *end example*]
757
-
758
- For the purpose of overload resolution, the functions that are
759
- introduced by a *using-declaration* into a derived class are treated as
760
- though they were members of the derived class. In particular, the
761
- implicit `this` parameter shall be treated as if it were a pointer to
762
- the derived class rather than to the base class. This has no effect on
763
- the type of the function, and in all other respects the function remains
764
- a member of the base class. Likewise, constructors that are introduced
765
- by a *using-declaration* are treated as though they were constructors of
766
- the derived class when looking up the constructors of the derived
767
- class ([[class.qual]]) or forming a set of overload candidates (
768
- [[over.match.ctor]], [[over.match.copy]], [[over.match.list]]). If such
769
- a constructor is selected to perform the initialization of an object of
770
- class type, all subobjects other than the base class from which the
771
- constructor originated are implicitly initialized (
772
- [[class.inhctor.init]]).
773
-
774
- In a *using-declarator* that does not name a constructor, all members of
775
- the set of introduced declarations shall be accessible. In a
776
- *using-declarator* that names a constructor, no access check is
777
- performed. In particular, if a derived class uses a *using-declarator*
778
- to access a member of a base class, the member name shall be accessible.
779
- If the name is that of an overloaded member function, then all functions
780
- named shall be accessible. The base class members mentioned by a
781
- *using-declarator* shall be visible in the scope of at least one of the
782
- direct base classes of the class where the *using-declarator* is
783
- specified.
784
-
785
- [*Note 6*:
786
-
787
- Because a *using-declarator* designates a base class member (and not a
788
- member subobject or a member function of a base class subobject), a
789
- *using-declarator* cannot be used to resolve inherited member
790
- ambiguities.
791
-
792
- [*Example 12*:
793
-
794
- ``` cpp
795
- struct A { int x(); };
796
- struct B : A { };
797
- struct C : A {
798
- using A::x;
799
- int x(int);
800
- };
801
-
802
- struct D : B, C {
803
- using C::x;
804
- int x(double);
805
- };
806
- int f(D* d) {
807
- return d->x(); // error: overload resolution selects A::x, but A is an ambiguous base class
808
- }
809
- ```
810
-
811
- — *end example*]
812
-
813
- — *end note*]
814
-
815
- A synonym created by a *using-declaration* has the usual accessibility
816
- for a *member-declaration*. A *using-declarator* that names a
817
- constructor does not create a synonym; instead, the additional
818
- constructors are accessible if they would be accessible when used to
819
- construct an object of the corresponding base class, and the
820
- accessibility of the *using-declaration* is ignored.
821
-
822
- [*Example 13*:
823
-
824
- ``` cpp
825
- class A {
826
- private:
827
- void f(char);
828
- public:
829
- void f(int);
830
- protected:
831
- void g();
832
- };
833
-
834
- class B : public A {
835
- using A::f; // error: A::f(char) is inaccessible
836
- public:
837
- using A::g; // B::g is a public synonym for A::g
838
- };
839
- ```
840
-
841
- — *end example*]
842
-
843
- If a *using-declarator* uses the keyword `typename` and specifies a
844
- dependent name ([[temp.dep]]), the name introduced by the
845
- *using-declaration* is treated as a *typedef-name* ([[dcl.typedef]]).
846
-
847
- ### Using directive <a id="namespace.udir">[[namespace.udir]]</a>
848
 
849
  ``` bnf
850
  using-directive:
851
- attribute-specifier-seqₒₚₜ 'using namespace' nested-name-specifierₒₚₜ namespace-name ';'
852
  ```
853
 
854
  A *using-directive* shall not appear in class scope, but may appear in
855
  namespace scope or in block scope.
856
 
@@ -861,12 +411,12 @@ only namespace names are considered, see 
861
  The optional *attribute-specifier-seq* appertains to the
862
  *using-directive*.
863
 
864
  A *using-directive* specifies that the names in the nominated namespace
865
  can be used in the scope in which the *using-directive* appears after
866
- the *using-directive*. During unqualified name lookup (
867
- [[basic.lookup.unqual]]), the names appear as if they were declared in
868
  the nearest enclosing namespace which contains both the
869
  *using-directive* and the nominated namespace.
870
 
871
  [*Note 2*: In this context, “contains” means “contains directly or
872
  indirectly”. — *end note*]
@@ -898,18 +448,18 @@ namespace A {
898
  void f3() {
899
  i = 5; // uses A::i
900
  }
901
  }
902
  void f4() {
903
- i = 5; // ill-formed; neither i is visible
904
  }
905
  ```
906
 
907
  — *end example*]
908
 
909
- For unqualified lookup ([[basic.lookup.unqual]]), the *using-directive*
910
- is transitive: if a scope contains a *using-directive* that nominates a
911
  second namespace that itself contains *using-directive*s, the effect is
912
  as if the *using-directive*s from the second namespace also appeared in
913
  the first.
914
 
915
  [*Note 3*: For qualified lookup, see 
@@ -958,25 +508,24 @@ namespace B {
958
  }
959
  ```
960
 
961
  — *end example*]
962
 
963
- If a namespace is extended ([[namespace.def]]) after a
964
- *using-directive* for that namespace is given, the additional members of
965
- the extended namespace and the members of namespaces nominated by
966
- *using-directive*s in the extending *namespace-definition* can be used
967
- after the extending *namespace-definition*.
 
 
968
 
969
  If name lookup finds a declaration for a name in two different
970
  namespaces, and the declarations do not declare the same entity and do
971
- not declare functions, the use of the name is ill-formed.
972
-
973
- [*Note 4*:
974
-
975
- In particular, the name of a variable, function or enumerator does not
976
- hide the name of a class or enumeration declared in a different
977
- namespace. For example,
978
 
979
  ``` cpp
980
  namespace A {
981
  class X { };
982
  extern "C" int g();
@@ -1008,11 +557,11 @@ and the relationships among the namespaces implied by the
1008
  *using-directive*s do not cause preference to be given to any of the
1009
  declarations found by the search. — *end note*]
1010
 
1011
  An ambiguity exists if the best match finds two functions with the same
1012
  signature, even if one is in a namespace reachable through
1013
- *using-directive*s in the namespace of the other.[^7]
1014
 
1015
  [*Example 3*:
1016
 
1017
  ``` cpp
1018
  namespace D {
 
4
  namespace can be used to access entities declared in that namespace;
5
  that is, the members of the namespace. Unlike other declarative regions,
6
  the definition of a namespace can be split over several parts of one or
7
  more translation units.
8
 
9
+ [*Note 1*: A namespace name with external linkage is exported if any of
10
+ its *namespace-definition*s is exported, or if it contains any
11
+ *export-declaration*s [[module.interface]]. A namespace is never
12
+ attached to a module, and never has module linkage even if it is not
13
+ exported. — *end note*]
14
+
15
+ [*Example 1*:
16
+
17
+ ``` cpp
18
+ export module M;
19
+ namespace N1 {} // N1 is not exported
20
+ export namespace N2 {} // N2 is exported
21
+ namespace N3 { export int n; } // N3 is exported
22
+ ```
23
+
24
+ — *end example*]
25
+
26
  The outermost declarative region of a translation unit is a namespace;
27
  see  [[basic.scope.namespace]].
28
 
29
  ### Namespace definition <a id="namespace.def">[[namespace.def]]</a>
30
 
 
41
  nested-namespace-definition
42
  ```
43
 
44
  ``` bnf
45
  named-namespace-definition:
46
+ inlineₒₚₜ namespace attribute-specifier-seqₒₚₜ identifier '{' namespace-body '}'
47
  ```
48
 
49
  ``` bnf
50
  unnamed-namespace-definition:
51
+ inlineₒₚₜ namespace attribute-specifier-seqₒₚₜ '{' namespace-body '}'
52
  ```
53
 
54
  ``` bnf
55
  nested-namespace-definition:
56
+ namespace enclosing-namespace-specifier '::' inlineₒₚₜ identifier '{' namespace-body '}'
57
  ```
58
 
59
  ``` bnf
60
  enclosing-namespace-specifier:
61
  identifier
62
+ enclosing-namespace-specifier '::' inlineₒₚₜ identifier
63
  ```
64
 
65
  ``` bnf
66
  namespace-body:
67
  declaration-seqₒₚₜ
68
  ```
69
 
70
+ Every *namespace-definition* shall appear at namespace scope
71
+ [[basic.scope.namespace]].
72
 
73
  In a *named-namespace-definition*, the *identifier* is the name of the
74
+ namespace. If the *identifier*, when looked up [[basic.lookup.unqual]],
75
+ refers to a *namespace-name* (but not a *namespace-alias*) that was
76
+ introduced in the namespace in which the *named-namespace-definition*
77
+ appears or that was introduced in a member of the inline namespace set
78
+ of that namespace, the *namespace-definition* *extends* the
79
+ previously-declared namespace. Otherwise, the *identifier* is introduced
80
+ as a *namespace-name* into the declarative region in which the
81
+ *named-namespace-definition* appears.
82
 
83
  Because a *namespace-definition* contains *declaration*s in its
84
  *namespace-body* and a *namespace-definition* is itself a *declaration*,
85
  it follows that *namespace-definition*s can be nested.
86
 
 
134
  appertains to the namespace being defined or extended.
135
 
136
  Members of an inline namespace can be used in most respects as though
137
  they were members of the enclosing namespace. Specifically, the inline
138
  namespace and its enclosing namespace are both added to the set of
139
+ associated namespaces used in argument-dependent lookup
140
+ [[basic.lookup.argdep]] whenever one of them is, and a *using-directive*
141
+ [[namespace.udir]] that names the inline namespace is implicitly
142
+ inserted into the enclosing namespace as for an unnamed namespace
143
+ [[namespace.unnamed]]. Furthermore, each member of the inline namespace
144
+ can subsequently be partially specialized [[temp.class.spec]],
145
+ explicitly instantiated [[temp.explicit]], or explicitly specialized
146
+ [[temp.expl.spec]] as though it were a member of the enclosing
147
+ namespace. Finally, looking up a name in the enclosing namespace via
148
+ explicit qualification [[namespace.qual]] will include members of the
149
+ inline namespace brought in by the *using-directive* even if there are
150
+ declarations of that name in the enclosing namespace.
151
 
152
  These properties are transitive: if a namespace `N` contains an inline
153
  namespace `M`, which in turn contains an inline namespace `O`, then the
154
  members of `O` can be used as though they were members of `M` or `N`.
155
  The *inline namespace set* of `N` is the transitive closure of all
 
160
 
161
  A *nested-namespace-definition* with an *enclosing-namespace-specifier*
162
  `E`, *identifier* `I` and *namespace-body* `B` is equivalent to
163
 
164
  ``` cpp
165
+ namespace E { \opt{inline} namespace I { B } }
166
  ```
167
 
168
+ where the optional `inline` is present if and only if the *identifier*
169
+ `I` is preceded by `inline`.
170
+
171
  [*Example 3*:
172
 
173
  ``` cpp
174
+ namespace A::inline B::C {
175
  int i;
176
  }
177
  ```
178
 
179
  The above has the same effect as:
180
 
181
  ``` cpp
182
  namespace A {
183
+ inline namespace B {
184
  namespace C {
185
  int i;
186
  }
187
  }
188
  }
 
193
  #### Unnamed namespaces <a id="namespace.unnamed">[[namespace.unnamed]]</a>
194
 
195
  An *unnamed-namespace-definition* behaves as if it were replaced by
196
 
197
  ``` bnf
198
+ inlineₒₚₜ namespace unique '{' '/* empty body */' '}'
199
+ using namespace unique ';'
200
+ namespace unique '{' namespace-body '}'
201
  ```
202
 
203
  where `inline` appears if and only if it appears in the
204
+ *unnamed-namespace-definition* and all occurrences of *`unique`* in a
205
  translation unit are replaced by the same identifier, and this
206
  identifier differs from all other identifiers in the translation unit.
207
  The optional *attribute-specifier-seq* in the
208
+ *unnamed-namespace-definition* appertains to *`unique`*.
209
 
210
  [*Example 1*:
211
 
212
  ``` cpp
213
  namespace { int i; } // unique::i
 
232
  — *end example*]
233
 
234
  #### Namespace member definitions <a id="namespace.memdef">[[namespace.memdef]]</a>
235
 
236
  A declaration in a namespace `N` (excluding declarations in nested
237
+ scopes) whose *declarator-id* is an *unqualified-id* [[dcl.meaning]],
238
+ whose *class-head-name* [[class.pre]] or *enum-head-name* [[dcl.enum]]
239
+ is an *identifier*, or whose *elaborated-type-specifier* is of the form
240
+ *class-key* *attribute-specifier-seq*ₒₚₜ *identifier*
241
+ [[dcl.type.elab]], or that is an *opaque-enum-declaration*, declares (or
242
+ redeclares) its *unqualified-id* or *identifier* as a member of `N`.
243
 
244
+ [*Note 1*: An explicit instantiation [[temp.explicit]] or explicit
245
+ specialization [[temp.expl.spec]] of a template does not introduce a
246
  name and thus may be declared using an *unqualified-id* in a member of
247
  the enclosing namespace set, if the primary template is declared in an
248
  inline namespace. — *end note*]
249
 
250
  [*Example 1*:
 
262
  ```
263
 
264
  — *end example*]
265
 
266
  Members of a named namespace can also be defined outside that namespace
267
+ by explicit qualification [[namespace.qual]] of the name being defined,
268
+ provided that the entity being defined was already declared in the
269
+ namespace and the definition appears after the point of declaration in a
270
+ namespace that encloses the declaration’s namespace.
271
 
272
  [*Example 2*:
273
 
274
  ``` cpp
275
  namespace Q {
 
288
  }
289
  ```
290
 
291
  — *end example*]
292
 
293
+ If a friend declaration in a non-local class first declares a class,
294
+ function, class template or function template[^10] the friend is a
295
+ member of the innermost enclosing namespace. The friend declaration does
296
+ not by itself make the name visible to unqualified lookup
297
+ [[basic.lookup.unqual]] or qualified lookup [[basic.lookup.qual]].
298
 
299
  [*Note 2*: The name of the friend will be visible in its namespace if a
300
  matching declaration is provided at namespace scope (either before or
301
  after the class definition granting friendship). — *end note*]
302
 
303
  If a friend function or function template is called, its name may be
304
  found by the name lookup that considers functions from namespaces and
305
+ classes associated with the types of the function arguments
306
+ [[basic.lookup.argdep]]. If the name in a friend declaration is neither
307
+ qualified nor a *template-id* and the declaration is a function or an
308
+ *elaborated-type-specifier*, the lookup to determine whether the entity
309
+ has been previously declared shall not consider any scopes outside the
310
+ innermost enclosing namespace.
311
 
312
+ [*Note 3*: The other forms of friend declarations cannot declare a new
313
+ member of the innermost enclosing namespace and thus follow the usual
314
+ lookup rules. — *end note*]
315
 
316
  [*Example 3*:
317
 
318
  ``` cpp
319
  // Assume f and g have not yet been declared.
 
359
  identifier
360
  ```
361
 
362
  ``` bnf
363
  namespace-alias-definition:
364
+ namespace identifier '=' qualified-namespace-specifier ';'
365
  ```
366
 
367
  ``` bnf
368
  qualified-namespace-specifier:
369
  nested-name-specifierₒₚₜ namespace-name
 
392
  namespace CWVLN = CWVLN;
393
  ```
394
 
395
  — *end example*]
396
 
397
+ ### Using namespace directive <a id="namespace.udir">[[namespace.udir]]</a>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398
 
399
  ``` bnf
400
  using-directive:
401
+ attribute-specifier-seqₒₚₜ using namespace nested-name-specifierₒₚₜ namespace-name ';'
402
  ```
403
 
404
  A *using-directive* shall not appear in class scope, but may appear in
405
  namespace scope or in block scope.
406
 
 
411
  The optional *attribute-specifier-seq* appertains to the
412
  *using-directive*.
413
 
414
  A *using-directive* specifies that the names in the nominated namespace
415
  can be used in the scope in which the *using-directive* appears after
416
+ the *using-directive*. During unqualified name lookup
417
+ [[basic.lookup.unqual]], the names appear as if they were declared in
418
  the nearest enclosing namespace which contains both the
419
  *using-directive* and the nominated namespace.
420
 
421
  [*Note 2*: In this context, “contains” means “contains directly or
422
  indirectly”. — *end note*]
 
448
  void f3() {
449
  i = 5; // uses A::i
450
  }
451
  }
452
  void f4() {
453
+ i = 5; // error: neither i is visible
454
  }
455
  ```
456
 
457
  — *end example*]
458
 
459
+ For unqualified lookup [[basic.lookup.unqual]], the *using-directive* is
460
+ transitive: if a scope contains a *using-directive* that nominates a
461
  second namespace that itself contains *using-directive*s, the effect is
462
  as if the *using-directive*s from the second namespace also appeared in
463
  the first.
464
 
465
  [*Note 3*: For qualified lookup, see 
 
508
  }
509
  ```
510
 
511
  — *end example*]
512
 
513
+ If a namespace is extended [[namespace.def]] after a *using-directive*
514
+ for that namespace is given, the additional members of the extended
515
+ namespace and the members of namespaces nominated by *using-directive*s
516
+ in the extending *namespace-definition* can be used after the extending
517
+ *namespace-definition*.
518
+
519
+ [*Note 4*:
520
 
521
  If name lookup finds a declaration for a name in two different
522
  namespaces, and the declarations do not declare the same entity and do
523
+ not declare functions or function templates, the use of the name is
524
+ ill-formed [[basic.lookup]]. In particular, the name of a variable,
525
+ function or enumerator does not hide the name of a class or enumeration
526
+ declared in a different namespace. For example,
 
 
 
527
 
528
  ``` cpp
529
  namespace A {
530
  class X { };
531
  extern "C" int g();
 
557
  *using-directive*s do not cause preference to be given to any of the
558
  declarations found by the search. — *end note*]
559
 
560
  An ambiguity exists if the best match finds two functions with the same
561
  signature, even if one is in a namespace reachable through
562
+ *using-directive*s in the namespace of the other.[^11]
563
 
564
  [*Example 3*:
565
 
566
  ``` cpp
567
  namespace D {