From Jason Turner

[namespace.udecl]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpc298zfy4/{from.md → to.md} +206 -96
tmp/tmpc298zfy4/{from.md → to.md} RENAMED
@@ -1,29 +1,47 @@
1
  ### The `using` declaration <a id="namespace.udecl">[[namespace.udecl]]</a>
2
 
3
- A *using-declaration* introduces a name into the declarative region in
4
- which the *using-declaration* appears.
5
-
6
  ``` bnf
7
  using-declaration:
8
- 'using typename'ₒₚₜ nested-name-specifier unqualified-id ';'
9
- 'using ::' unqualified-id ';'
10
  ```
11
 
12
- The member name specified in a *using-declaration* is declared in the
13
- declarative region in which the *using-declaration* appears. Only the
14
- specified name is so declared; specifying an enumeration name in a
15
- *using-declaration* does not declare its enumerators in the
16
- *using-declaration*’s declarative region. If a *using-declaration* names
17
- a constructor ([[class.qual]]), it implicitly declares a set of
18
- constructors in the class in which the *using-declaration* appears (
19
- [[class.inhctor]]); otherwise the name specified in a
20
- *using-declaration* is a synonym for a set of declarations in another
21
- namespace or class.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  Every *using-declaration* is a *declaration* and a *member-declaration*
24
- and so can be used in a class definition.
 
 
25
 
26
  ``` cpp
27
  struct B {
28
  void f(char);
29
  void g(char);
@@ -36,16 +54,32 @@ struct D : B {
36
  void f(int) { f('c'); } // calls B::f(char)
37
  void g(int) { g('c'); } // recursively calls D::g(int)
38
  };
39
  ```
40
 
41
- In a *using-declaration* used as a *member-declaration*, the
42
- *nested-name-specifier* shall name a base class of the class being
43
- defined. If such a *using-declaration* names a constructor, the
44
- *nested-name-specifier* shall name a direct base class of the class
45
- being defined; otherwise it introduces the set of declarations found by
46
- member name lookup ([[class.member.lookup]],  [[class.qual]]).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  ``` cpp
49
  class C {
50
  int g();
51
  };
@@ -56,25 +90,30 @@ class D2 : public B {
56
  using B::x; // OK: x is a union member of base B
57
  using C::g; // error: C isn't a base of D2
58
  };
59
  ```
60
 
61
- Since destructors do not have names, a *using-declaration* cannot refer
62
- to a destructor for a base class. Since specializations of member
63
- templates for conversion functions are not found by name lookup, they
64
- are not considered when a *using-declaration* specifies a conversion
65
- function ([[temp.mem]]). If an assignment operator brought from a base
66
- class into a derived class scope has the signature of a copy/move
67
- assignment operator for the derived class ([[class.copy]]), the
 
 
 
 
68
  *using-declaration* does not by itself suppress the implicit declaration
69
- of the derived class assignment operator; the copy/move assignment
70
- operator from the base class is hidden or overridden by the
71
- implicitly-declared copy/move assignment operator of the derived class,
72
- as described below.
73
 
74
  A *using-declaration* shall not name a *template-id*.
75
 
 
 
76
  ``` cpp
77
  struct A {
78
  template <class T> void f(T);
79
  template <class T> struct X { };
80
  };
@@ -82,34 +121,39 @@ struct B : A {
82
  using A::f<double>; // ill-formed
83
  using A::X<int>; // ill-formed
84
  };
85
  ```
86
 
 
 
87
  A *using-declaration* shall not name a namespace.
88
 
89
  A *using-declaration* shall not name a scoped enumerator.
90
 
91
- A *using-declaration* for a class member shall be a
92
  *member-declaration*.
93
 
 
 
94
  ``` cpp
95
  struct X {
96
  int i;
97
  static int s;
98
  };
99
 
100
  void f() {
101
- using X::i; // error: X::i is a class member
102
- // and this is not a member declaration.
103
- using X::s; // error: X::s is a class member
104
- // and this is not a member declaration.
105
  }
106
  ```
107
 
 
 
108
  Members declared by a *using-declaration* can be referred to by explicit
109
- qualification just like other member names ([[namespace.qual]]). In a
110
- *using-declaration*, a prefix `::` refers to the global namespace.
 
111
 
112
  ``` cpp
113
  void f();
114
 
115
  namespace A {
@@ -126,78 +170,83 @@ void h()
126
  X::f(); // calls ::f
127
  X::g(); // calls A::g
128
  }
129
  ```
130
 
 
 
131
  A *using-declaration* is a *declaration* and can therefore be used
132
  repeatedly where (and only where) multiple declarations are allowed.
133
 
 
 
134
  ``` cpp
135
  namespace A {
136
  int i;
137
  }
138
 
139
  namespace A1 {
140
- using A::i;
141
- using A::i; // OK: double declaration
142
- }
143
-
144
- void f() {
145
- using A::i;
146
- using A::i; // error: double declaration
147
  }
148
 
149
  struct B {
150
  int i;
151
  };
152
 
153
  struct X : B {
154
- using B::i;
155
- using B::i; // error: double member declaration
156
  };
157
  ```
158
 
159
- Members added to the namespace after the *using-declaration* are not
160
- considered when a use of the name is made. Thus, additional overloads
161
- added after the *using-declaration* are ignored, but default function
162
- arguments ([[dcl.fct.default]]), default template arguments (
 
 
 
 
163
  [[temp.param]]), and template specializations ([[temp.class.spec]],
164
- [[temp.expl.spec]]) are considered.
 
 
165
 
166
  ``` cpp
167
  namespace A {
168
  void f(int);
169
  }
170
 
171
- using A::f; // f is a synonym for A::f;
172
- // that is, for A::f(int).
173
  namespace A {
174
  void f(char);
175
  }
176
 
177
  void foo() {
178
- f('a'); // calls f(int),
179
- } // even though f(char) exists.
180
 
181
  void bar() {
182
- using A::f; // f is a synonym for A::f;
183
- // that is, for A::f(int) and A::f(char).
184
  f('a'); // calls f(char)
185
  }
186
  ```
187
 
188
- Partial specializations of class templates are found by looking up the
189
- primary class template and then considering all partial specializations
190
- of that template. If a *using-declaration* names a class template,
191
- partial specializations introduced after the *using-declaration* are
192
- effectively visible because the primary template is visible (
193
- [[temp.class.spec]]).
 
 
194
 
195
  Since a *using-declaration* is a declaration, the restrictions on
196
  declarations of the same name in the same declarative region (
197
  [[basic.scope]]) also apply to *using-declaration*s.
198
 
 
 
199
  ``` cpp
200
  namespace A {
201
  int x;
202
  }
203
 
@@ -224,22 +273,29 @@ void func() {
224
  x = 99; // assigns to A::x
225
  struct x x1; // x1 has class type B::x
226
  }
227
  ```
228
 
 
 
229
  If a function declaration in namespace scope or block scope has the same
230
  name and the same parameter-type-list ([[dcl.fct]]) as a function
231
  introduced by a *using-declaration*, and the declarations do not declare
232
  the same function, the program is ill-formed. If a function template
233
  declaration in namespace scope has the same name, parameter-type-list,
234
  return type, and template parameter list as a function template
235
- introduced by a *using-declaration*, the program is ill-formed. Two
236
- *using-declaration*s may introduce functions with the same name and the
237
- same parameter-type-list. If, for a call to an unqualified function
 
 
 
238
  name, function overload resolution selects the functions introduced by
239
  such *using-declaration*s, the function call is ill-formed.
240
 
 
 
241
  ``` cpp
242
  namespace B {
243
  void f(int);
244
  void f(double);
245
  }
@@ -256,17 +312,23 @@ void h() {
256
  f(1); // error: ambiguous: B::f(int) or C::f(int)?
257
  void f(int); // error: f(int) conflicts with C::f(int) and B::f(int)
258
  }
259
  ```
260
 
261
- When a *using-declaration* brings names from a base class into a derived
262
- class scope, member functions and member function templates in the
 
 
 
 
263
  derived class override and/or hide member functions and member function
264
  templates with the same name, parameter-type-list ([[dcl.fct]]),
265
  cv-qualification, and *ref-qualifier* (if any) in a base class (rather
266
- than conflicting). For *using-declaration*s that name a constructor,
267
- see  [[class.inhctor]].
 
 
268
 
269
  ``` cpp
270
  struct B {
271
  virtual void f(int);
272
  virtual void f(char);
@@ -290,32 +352,70 @@ void k(D* p)
290
  p->f(1); // calls D::f(int)
291
  p->f('a'); // calls B::f(char)
292
  p->g(1); // calls B::g(int)
293
  p->g('a'); // calls D::g(char)
294
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295
  ```
296
 
297
- For the purpose of overload resolution, the functions which are
298
- introduced by a *using-declaration* into a derived class will be treated
299
- as though they were members of the derived class. In particular, the
 
 
300
  implicit `this` parameter shall be treated as if it were a pointer to
301
  the derived class rather than to the base class. This has no effect on
302
  the type of the function, and in all other respects the function remains
303
- a member of the base class.
304
-
305
- The access rules for inheriting constructors are specified in 
306
- [[class.inhctor]]; otherwise all instances of the name mentioned in a
307
- *using-declaration* shall be accessible. In particular, if a derived
308
- class uses a *using-declaration* to access a member of a base class, the
309
- member name shall be accessible. If the name is that of an overloaded
310
- member function, then all functions named shall be accessible. The base
311
- class members mentioned by a *using-declaration* shall be visible in the
312
- scope of at least one of the direct base classes of the class where the
313
- *using-declaration* is specified. Because a *using-declaration*
314
- designates a base class member (and not a member subobject or a member
315
- function of a base class subobject), a *using-declaration* cannot be
316
- used to resolve inherited member ambiguities. For example,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
 
318
  ``` cpp
319
  struct A { int x(); };
320
  struct B : A { };
321
  struct C : A {
@@ -326,18 +426,26 @@ struct C : A {
326
  struct D : B, C {
327
  using C::x;
328
  int x(double);
329
  };
330
  int f(D* d) {
331
- return d->x(); // ambiguous: B::x or C::x
332
  }
333
  ```
334
 
335
- The alias created by the *using-declaration* has the usual accessibility
336
- for a *member-declaration*. A *using-declaration* that names a
337
- constructor does not create aliases; see  [[class.inhctor]] for the
338
- pertinent accessibility rules.
 
 
 
 
 
 
 
 
339
 
340
  ``` cpp
341
  class A {
342
  private:
343
  void f(char);
@@ -352,9 +460,11 @@ class B : public A {
352
  public:
353
  using A::g; // B::g is a public synonym for A::g
354
  };
355
  ```
356
 
357
- If a *using-declaration* uses the keyword `typename` and specifies a
 
 
358
  dependent name ([[temp.dep]]), the name introduced by the
359
  *using-declaration* is treated as a *typedef-name* ([[dcl.typedef]]).
360
 
 
1
  ### The `using` declaration <a id="namespace.udecl">[[namespace.udecl]]</a>
2
 
 
 
 
3
  ``` bnf
4
  using-declaration:
5
+ 'using' using-declarator-list ';'
 
6
  ```
7
 
8
+ ``` bnf
9
+ using-declarator-list:
10
+ using-declarator '...'ₒₚₜ
11
+ using-declarator-list ',' using-declarator '...'ₒₚₜ
12
+ ```
13
+
14
+ ``` bnf
15
+ using-declarator:
16
+ 'typename'ₒₚₜ nested-name-specifier unqualified-id
17
+ ```
18
+
19
+ Each *using-declarator* in a *using-declaration* [^6] introduces a set
20
+ of declarations into the declarative region in which the
21
+ *using-declaration* appears. The set of declarations introduced by the
22
+ *using-declarator* is found by performing qualified name lookup (
23
+ [[basic.lookup.qual]], [[class.member.lookup]]) for the name in the
24
+ *using-declarator*, excluding functions that are hidden as described
25
+ below. If the *using-declarator* does not name a constructor, the
26
+ *unqualified-id* is declared in the declarative region in which the
27
+ *using-declaration* appears as a synonym for each declaration introduced
28
+ by the *using-declarator*.
29
+
30
+ [*Note 1*: Only the specified name is so declared; specifying an
31
+ enumeration name in a *using-declaration* does not declare its
32
+ enumerators in the *using-declaration*'s declarative
33
+ region. — *end note*]
34
+
35
+ If the *using-declarator* names a constructor, it declares that the
36
+ class *inherits* the set of constructor declarations introduced by the
37
+ *using-declarator* from the nominated base class.
38
 
39
  Every *using-declaration* is a *declaration* and a *member-declaration*
40
+ and can therefore be used in a class definition.
41
+
42
+ [*Example 1*:
43
 
44
  ``` cpp
45
  struct B {
46
  void f(char);
47
  void g(char);
 
54
  void f(int) { f('c'); } // calls B::f(char)
55
  void g(int) { g('c'); } // recursively calls D::g(int)
56
  };
57
  ```
58
 
59
+ *end example*]
60
+
61
+ In a *using-declaration* used as a *member-declaration*, each
62
+ *using-declarator*'s *nested-name-specifier* shall name a base class of
63
+ the class being defined. If a *using-declarator* names a constructor,
64
+ its *nested-name-specifier* shall name a direct base class of the class
65
+ being defined.
66
+
67
+ [*Example 2*:
68
+
69
+ ``` cpp
70
+ template <typename... bases>
71
+ struct X : bases... {
72
+ using bases::g...;
73
+ };
74
+
75
+ X<B, D> x; // OK: B::g and D::g introduced
76
+ ```
77
+
78
+ — *end example*]
79
+
80
+ [*Example 3*:
81
 
82
  ``` cpp
83
  class C {
84
  int g();
85
  };
 
90
  using B::x; // OK: x is a union member of base B
91
  using C::g; // error: C isn't a base of D2
92
  };
93
  ```
94
 
95
+ *end example*]
96
+
97
+ [*Note 2*: Since destructors do not have names, a *using-declaration*
98
+ cannot refer to a destructor for a base class. Since specializations of
99
+ member templates for conversion functions are not found by name lookup,
100
+ they are not considered when a *using-declaration* specifies a
101
+ conversion function ([[temp.mem]]). — *end note*]
102
+
103
+ If a constructor or assignment operator brought from a base class into a
104
+ derived class has the signature of a copy/move constructor or assignment
105
+ operator for the derived class ([[class.copy]]), the
106
  *using-declaration* does not by itself suppress the implicit declaration
107
+ of the derived class member; the member from the base class is hidden or
108
+ overridden by the implicitly-declared copy/move constructor or
109
+ assignment operator of the derived class, as described below.
 
110
 
111
  A *using-declaration* shall not name a *template-id*.
112
 
113
+ [*Example 4*:
114
+
115
  ``` cpp
116
  struct A {
117
  template <class T> void f(T);
118
  template <class T> struct X { };
119
  };
 
121
  using A::f<double>; // ill-formed
122
  using A::X<int>; // ill-formed
123
  };
124
  ```
125
 
126
+ — *end example*]
127
+
128
  A *using-declaration* shall not name a namespace.
129
 
130
  A *using-declaration* shall not name a scoped enumerator.
131
 
132
+ A *using-declaration* that names a class member shall be a
133
  *member-declaration*.
134
 
135
+ [*Example 5*:
136
+
137
  ``` cpp
138
  struct X {
139
  int i;
140
  static int s;
141
  };
142
 
143
  void f() {
144
+ using X::i; // error: X::i is a class member and this is not a member declaration.
145
+ using X::s; // error: X::s is a class member and this is not a member declaration.
 
 
146
  }
147
  ```
148
 
149
+ — *end example*]
150
+
151
  Members declared by a *using-declaration* can be referred to by explicit
152
+ qualification just like other member names ([[namespace.qual]]).
153
+
154
+ [*Example 6*:
155
 
156
  ``` cpp
157
  void f();
158
 
159
  namespace A {
 
170
  X::f(); // calls ::f
171
  X::g(); // calls A::g
172
  }
173
  ```
174
 
175
+ — *end example*]
176
+
177
  A *using-declaration* is a *declaration* and can therefore be used
178
  repeatedly where (and only where) multiple declarations are allowed.
179
 
180
+ [*Example 7*:
181
+
182
  ``` cpp
183
  namespace A {
184
  int i;
185
  }
186
 
187
  namespace A1 {
188
+ using A::i, A::i; // OK: double declaration
 
 
 
 
 
 
189
  }
190
 
191
  struct B {
192
  int i;
193
  };
194
 
195
  struct X : B {
196
+ using B::i, B::i; // error: double member declaration
 
197
  };
198
  ```
199
 
200
+ *end example*]
201
+
202
+ [*Note 3*: For a *using-declaration* whose *nested-name-specifier*
203
+ names a namespace, members added to the namespace after the
204
+ *using-declaration* are not in the set of introduced declarations, so
205
+ they are not considered when a use of the name is made. Thus, additional
206
+ overloads added after the *using-declaration* are ignored, but default
207
+ function arguments ([[dcl.fct.default]]), default template arguments (
208
  [[temp.param]]), and template specializations ([[temp.class.spec]],
209
+ [[temp.expl.spec]]) are considered. — *end note*]
210
+
211
+ [*Example 8*:
212
 
213
  ``` cpp
214
  namespace A {
215
  void f(int);
216
  }
217
 
218
+ using A::f; // f is a synonym for A::f; that is, for A::f(int).
 
219
  namespace A {
220
  void f(char);
221
  }
222
 
223
  void foo() {
224
+ f('a'); // calls f(int), even though f(char) exists.
225
+ }
226
 
227
  void bar() {
228
+ using A::f; // f is a synonym for A::f; that is, for A::f(int) and A::f(char).
 
229
  f('a'); // calls f(char)
230
  }
231
  ```
232
 
233
+ *end example*]
234
+
235
+ [*Note 4*: Partial specializations of class templates are found by
236
+ looking up the primary class template and then considering all partial
237
+ specializations of that template. If a *using-declaration* names a class
238
+ template, partial specializations introduced after the
239
+ *using-declaration* are effectively visible because the primary template
240
+ is visible ([[temp.class.spec]]). — *end note*]
241
 
242
  Since a *using-declaration* is a declaration, the restrictions on
243
  declarations of the same name in the same declarative region (
244
  [[basic.scope]]) also apply to *using-declaration*s.
245
 
246
+ [*Example 9*:
247
+
248
  ``` cpp
249
  namespace A {
250
  int x;
251
  }
252
 
 
273
  x = 99; // assigns to A::x
274
  struct x x1; // x1 has class type B::x
275
  }
276
  ```
277
 
278
+ — *end example*]
279
+
280
  If a function declaration in namespace scope or block scope has the same
281
  name and the same parameter-type-list ([[dcl.fct]]) as a function
282
  introduced by a *using-declaration*, and the declarations do not declare
283
  the same function, the program is ill-formed. If a function template
284
  declaration in namespace scope has the same name, parameter-type-list,
285
  return type, and template parameter list as a function template
286
+ introduced by a *using-declaration*, the program is ill-formed.
287
+
288
+ [*Note 5*:
289
+
290
+ Two *using-declaration*s may introduce functions with the same name and
291
+ the same parameter-type-list. If, for a call to an unqualified function
292
  name, function overload resolution selects the functions introduced by
293
  such *using-declaration*s, the function call is ill-formed.
294
 
295
+ [*Example 10*:
296
+
297
  ``` cpp
298
  namespace B {
299
  void f(int);
300
  void f(double);
301
  }
 
312
  f(1); // error: ambiguous: B::f(int) or C::f(int)?
313
  void f(int); // error: f(int) conflicts with C::f(int) and B::f(int)
314
  }
315
  ```
316
 
317
+ *end example*]
318
+
319
+ — *end note*]
320
+
321
+ When a *using-declarator* brings declarations from a base class into a
322
+ derived class, member functions and member function templates in the
323
  derived class override and/or hide member functions and member function
324
  templates with the same name, parameter-type-list ([[dcl.fct]]),
325
  cv-qualification, and *ref-qualifier* (if any) in a base class (rather
326
+ than conflicting). Such hidden or overridden declarations are excluded
327
+ from the set of declarations introduced by the *using-declarator*.
328
+
329
+ [*Example 11*:
330
 
331
  ``` cpp
332
  struct B {
333
  virtual void f(int);
334
  virtual void f(char);
 
352
  p->f(1); // calls D::f(int)
353
  p->f('a'); // calls B::f(char)
354
  p->g(1); // calls B::g(int)
355
  p->g('a'); // calls D::g(char)
356
  }
357
+
358
+ struct B1 {
359
+ B1(int);
360
+ };
361
+
362
+ struct B2 {
363
+ B2(int);
364
+ };
365
+
366
+ struct D1 : B1, B2 {
367
+ using B1::B1;
368
+ using B2::B2;
369
+ };
370
+ D1 d1(0); // ill-formed: ambiguous
371
+
372
+ struct D2 : B1, B2 {
373
+ using B1::B1;
374
+ using B2::B2;
375
+ D2(int); // OK: D2::D2(int) hides B1::B1(int) and B2::B2(int)
376
+ };
377
+ D2 d2(0); // calls D2::D2(int)
378
  ```
379
 
380
+ *end example*]
381
+
382
+ For the purpose of overload resolution, the functions that are
383
+ introduced by a *using-declaration* into a derived class are treated as
384
+ though they were members of the derived class. In particular, the
385
  implicit `this` parameter shall be treated as if it were a pointer to
386
  the derived class rather than to the base class. This has no effect on
387
  the type of the function, and in all other respects the function remains
388
+ a member of the base class. Likewise, constructors that are introduced
389
+ by a *using-declaration* are treated as though they were constructors of
390
+ the derived class when looking up the constructors of the derived
391
+ class ([[class.qual]]) or forming a set of overload candidates (
392
+ [[over.match.ctor]], [[over.match.copy]], [[over.match.list]]). If such
393
+ a constructor is selected to perform the initialization of an object of
394
+ class type, all subobjects other than the base class from which the
395
+ constructor originated are implicitly initialized (
396
+ [[class.inhctor.init]]).
397
+
398
+ In a *using-declarator* that does not name a constructor, all members of
399
+ the set of introduced declarations shall be accessible. In a
400
+ *using-declarator* that names a constructor, no access check is
401
+ performed. In particular, if a derived class uses a *using-declarator*
402
+ to access a member of a base class, the member name shall be accessible.
403
+ If the name is that of an overloaded member function, then all functions
404
+ named shall be accessible. The base class members mentioned by a
405
+ *using-declarator* shall be visible in the scope of at least one of the
406
+ direct base classes of the class where the *using-declarator* is
407
+ specified.
408
+
409
+ [*Note 6*:
410
+
411
+ Because a *using-declarator* designates a base class member (and not a
412
+ member subobject or a member function of a base class subobject), a
413
+ *using-declarator* cannot be used to resolve inherited member
414
+ ambiguities.
415
+
416
+ [*Example 12*:
417
 
418
  ``` cpp
419
  struct A { int x(); };
420
  struct B : A { };
421
  struct C : A {
 
426
  struct D : B, C {
427
  using C::x;
428
  int x(double);
429
  };
430
  int f(D* d) {
431
+ return d->x(); // error: overload resolution selects A::x, but A is an ambiguous base class
432
  }
433
  ```
434
 
435
+ *end example*]
436
+
437
+ *end note*]
438
+
439
+ A synonym created by a *using-declaration* has the usual accessibility
440
+ for a *member-declaration*. A *using-declarator* that names a
441
+ constructor does not create a synonym; instead, the additional
442
+ constructors are accessible if they would be accessible when used to
443
+ construct an object of the corresponding base class, and the
444
+ accessibility of the *using-declaration* is ignored.
445
+
446
+ [*Example 13*:
447
 
448
  ``` cpp
449
  class A {
450
  private:
451
  void f(char);
 
460
  public:
461
  using A::g; // B::g is a public synonym for A::g
462
  };
463
  ```
464
 
465
+ *end example*]
466
+
467
+ If a *using-declarator* uses the keyword `typename` and specifies a
468
  dependent name ([[temp.dep]]), the name introduced by the
469
  *using-declaration* is treated as a *typedef-name* ([[dcl.typedef]]).
470