From Jason Turner

[class.access]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp4yjcc4r4/{from.md → to.md} +177 -138
tmp/tmp4yjcc4r4/{from.md → to.md} RENAMED
@@ -1,20 +1,24 @@
1
  ## Member access control <a id="class.access">[[class.access]]</a>
2
 
 
 
3
  A member of a class can be
4
 
5
- - private; that is, its name can be used only by members and friends of
6
- the class in which it is declared.
7
- - protected; that is, its name can be used only by members and friends
8
- of the class in which it is declared, by classes derived from that
9
- class, and by their friends (see  [[class.protected]]).
10
- - public; that is, its name can be used anywhere without access
11
- restriction.
12
 
13
- A member of a class can also access all the names to which the class has
14
- access. A local class of a member function may access the same names
15
- that the member function itself may access.[^11]
 
 
 
16
 
17
  Members of a class defined with the keyword `class` are `private` by
18
  default. Members of a class defined with the keywords `struct` or
19
  `union` are public by default.
20
 
@@ -30,61 +34,77 @@ struct S {
30
  };
31
  ```
32
 
33
  — *end example*]
34
 
35
- Access control is applied uniformly to all names, whether the names are
36
- referred to from declarations or expressions.
37
 
38
- [*Note 1*: Access control applies to names nominated by friend
39
  declarations [[class.friend]] and *using-declaration*s
40
  [[namespace.udecl]]. — *end note*]
41
 
42
- In the case of overloaded function names, access control is applied to
43
- the function selected by overload resolution.
 
44
 
45
- [*Note 2*:
46
 
47
- Because access control applies to names, if access control is applied to
48
- a typedef name, only the accessibility of the typedef name itself is
49
- considered. The accessibility of the entity referred to by the typedef
50
- is not considered. For example,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  ``` cpp
53
  class A {
54
  class B { };
55
  public:
56
  typedef B BB;
57
  };
58
 
59
  void f() {
60
- A::BB x; // OK, typedef name A::BB is public
61
  A::B y; // access error, A::B is private
62
  }
63
  ```
64
 
65
  — *end note*]
66
 
67
- [*Note 3*: Access to members and base classes is controlled, not their
68
- visibility [[basic.scope.hiding]]. Names of members are still visible,
69
- and implicit conversions to base classes are still considered, when
70
- those members and base classes are inaccessible. — *end note*]
71
 
72
  The interpretation of a given construct is established without regard to
73
  access control. If the interpretation established makes use of
74
- inaccessible member names or base classes, the construct is ill-formed.
75
 
76
- All access controls in [[class.access]] affect the ability to access a
77
- class member name from the declaration of a particular entity, including
78
  parts of the declaration preceding the name of the entity being declared
79
  and, if the entity is a class, the definitions of members of the class
80
  appearing outside the class’s *member-specification*.
81
 
82
- [*Note 4*: This access also applies to implicit references to
83
  constructors, conversion functions, and destructors. — *end note*]
84
 
85
- [*Example 2*:
86
 
87
  ``` cpp
88
  class A {
89
  typedef int I; // private member
90
  I f();
@@ -115,21 +135,20 @@ is as the return type of a member of class `A`. Similarly, the use of
115
  `A`, so checking of *base-specifier*s must be deferred until the entire
116
  *base-specifier-list* has been seen.
117
 
118
  — *end example*]
119
 
120
- The names in a default argument [[dcl.fct.default]] are bound at the
121
- point of declaration, and access is checked at that point rather than at
122
- any points of use of the default argument. Access checking for default
123
- arguments in function templates and in member functions of class
124
- templates is performed as described in  [[temp.inst]].
125
 
126
- The names in a default *template-argument* [[temp.param]] have their
127
- access checked in the context in which they appear rather than at any
128
- points of use of the default *template-argument*.
129
 
130
- [*Example 3*:
131
 
132
  ``` cpp
133
  class B { };
134
  template <class T> class C {
135
  protected:
@@ -144,12 +163,12 @@ D <C<B> >* d; // access error, C::TT is protected
144
 
145
  — *end example*]
146
 
147
  ### Access specifiers <a id="class.access.spec">[[class.access.spec]]</a>
148
 
149
- Member declarations can be labeled by an *access-specifier* (
150
- [[class.derived]]):
151
 
152
  ``` bnf
153
  access-specifier ':' member-specificationₒₚₜ
154
  ```
155
 
@@ -187,13 +206,10 @@ public:
187
  };
188
  ```
189
 
190
  — *end example*]
191
 
192
- [*Note 1*: The effect of access control on the order of allocation of
193
- data members is specified in  [[expr.rel]]. — *end note*]
194
-
195
  When a member is redeclared within its class definition, the access
196
  specified at its redeclaration shall be the same as at its initial
197
  declaration.
198
 
199
  [*Example 3*:
@@ -208,11 +224,11 @@ private:
208
  };
209
  ```
210
 
211
  — *end example*]
212
 
213
- [*Note 2*: In a derived class, the lookup of a base class name will
214
  find the injected-class-name instead of the name of the base class in
215
  the scope in which it was declared. The injected-class-name might be
216
  less accessible than the name of the base class in the scope in which it
217
  was declared. — *end note*]
218
 
@@ -267,17 +283,17 @@ Here `B` is a public base of `D2`, `D4`, and `D6`, a private base of
267
 
268
  — *end example*]
269
 
270
  [*Note 1*:
271
 
272
- A member of a private base class might be inaccessible as an inherited
273
- member name, but accessible directly. Because of the rules on pointer
274
- conversions [[conv.ptr]] and explicit casts ([[expr.type.conv]],
275
- [[expr.static.cast]], [[expr.cast]]), a conversion from a pointer to a
276
- derived class to a pointer to an inaccessible base class might be
277
- ill-formed if an implicit conversion is used, but well-formed if an
278
- explicit cast is used. For example,
279
 
280
  ``` cpp
281
  class B {
282
  public:
283
  int mi; // non-static member
@@ -296,24 +312,24 @@ void DD::f() {
296
  b.mi = 3; // OK (b.mi is different from this->mi)
297
  b.si = 3; // OK (b.si is different from this->si)
298
  ::B::si = 3; // OK
299
  ::B* bp1 = this; // error: B is a private base class
300
  ::B* bp2 = (::B*)this; // OK with cast
301
- bp2->mi = 3; // OK: access through a pointer to B.
302
  }
303
  ```
304
 
305
  — *end note*]
306
 
307
  A base class `B` of `N` is *accessible* at *R*, if
308
 
309
  - an invented public member of `B` would be a public member of `N`, or
310
- - *R* occurs in a member or friend of class `N`, and an invented public
311
- member of `B` would be a private or protected member of `N`, or
312
- - *R* occurs in a member or friend of a class `P` derived from `N`, and
313
- an invented public member of `B` would be a private or protected
314
- member of `P`, or
315
  - there exists a class `S` such that `B` is a base class of `S`
316
  accessible at *R* and `S` is a base class of `N` accessible at *R*.
317
 
318
  [*Example 2*:
319
 
@@ -337,20 +353,20 @@ class N: private S {
337
  ```
338
 
339
  — *end example*]
340
 
341
  If a base class is accessible, one can implicitly convert a pointer to a
342
- derived class to a pointer to that base class ([[conv.ptr]],
343
- [[conv.mem]]).
344
 
345
  [*Note 2*: It follows that members and friends of a class `X` can
346
  implicitly convert an `X*` to a pointer to a private or protected
347
  immediate base class of `X`. — *end note*]
348
 
349
  The access to a member is affected by the class in which the member is
350
- named. This naming class is the class in which the member name was
351
- looked up and found.
352
 
353
  [*Note 3*: This class can be explicit, e.g., when a *qualified-id* is
354
  used, or implicit, e.g., when a class member access operator
355
  [[expr.ref]] is used (including cases where an implicit “`this->`” is
356
  added). If both a class member access operator and a *qualified-id* are
@@ -359,15 +375,15 @@ is the class denoted by the *nested-name-specifier* of the
359
  *qualified-id* (that is, `T`). — *end note*]
360
 
361
  A member `m` is accessible at the point *R* when named in class `N` if
362
 
363
  - `m` as a member of `N` is public, or
364
- - `m` as a member of `N` is private, and *R* occurs in a member or
365
- friend of class `N`, or
366
- - `m` as a member of `N` is protected, and *R* occurs in a member or
367
- friend of class `N`, or in a member of a class `P` derived from `N`,
368
- where `m` as a member of `P` is public, private, or protected, or
369
  - there exists a base class `B` of `N` that is accessible at *R*, and
370
  `m` is accessible at *R* when named in class `B`.
371
  \[*Example 3*:
372
  ``` cpp
373
  class B;
@@ -376,11 +392,11 @@ A member `m` is accessible at the point *R* when named in class `N` if
376
  int i;
377
  friend void f(B*);
378
  };
379
  class B : public A { };
380
  void f(B* p) {
381
- p->i = 1; // OK: B* can be implicitly converted to A*, and f has access to i in A
382
  }
383
  ```
384
 
385
  — *end example*]
386
 
@@ -394,14 +410,14 @@ to the naming class of the right operand.
394
  member be accessible as named. — *end note*]
395
 
396
  ### Friends <a id="class.friend">[[class.friend]]</a>
397
 
398
  A friend of a class is a function or class that is given permission to
399
- use the private and protected member names from the class. A class
400
- specifies its friends, if any, by way of friend declarations. Such
401
- declarations give special access rights to the friends, but they do not
402
- make the nominated friends members of the befriending class.
403
 
404
  [*Example 1*:
405
 
406
  The following example illustrates the differences between members and
407
  friends:
@@ -424,26 +440,26 @@ void f() {
424
  }
425
  ```
426
 
427
  — *end example*]
428
 
429
- Declaring a class to be a friend implies that the names of private and
430
- protected members from the class granting friendship can be accessed in
431
- the *base-specifier*s and member declarations of the befriended class.
432
 
433
  [*Example 2*:
434
 
435
  ``` cpp
436
  class A {
437
  class B { };
438
  friend class X;
439
  };
440
 
441
- struct X : A::B { // OK: A::B accessible to friend
442
- A::B mx; // OK: A::B accessible to member of friend
443
  class Y {
444
- A::B my; // OK: A::B accessible to nested member of friend
445
  };
446
  };
447
  ```
448
 
449
  — *end example*]
@@ -465,74 +481,62 @@ class Z {
465
  };
466
  ```
467
 
468
  — *end example*]
469
 
470
- A class shall not be defined in a friend declaration.
471
-
472
- [*Example 4*:
473
-
474
- ``` cpp
475
- class A {
476
- friend class B { }; // error: cannot define class in friend declaration
477
- };
478
- ```
479
-
480
- — *end example*]
481
-
482
  A friend declaration that does not declare a function shall have one of
483
  the following forms:
484
 
485
  ``` bnf
486
  friend elaborated-type-specifier ';'
487
  friend simple-type-specifier ';'
488
  friend typename-specifier ';'
489
  ```
490
 
491
- [*Note 1*: A friend declaration may be the *declaration* in a
492
- *template-declaration* ([[temp.pre]], [[temp.friend]]). — *end note*]
493
 
494
  If the type specifier in a `friend` declaration designates a (possibly
495
  cv-qualified) class type, that class is declared as a friend; otherwise,
496
  the friend declaration is ignored.
497
 
498
- [*Example 5*:
499
 
500
  ``` cpp
501
  class C;
502
  typedef C Ct;
503
 
504
  class X1 {
505
- friend C; // OK: class C is a friend
506
  };
507
 
508
  class X2 {
509
- friend Ct; // OK: class C is a friend
510
- friend D; // error: no type-name D in scope
511
- friend class D; // OK: elaborated-type-specifier declares new class
512
  };
513
 
514
  template <typename T> class R {
515
  friend T;
516
  };
517
 
518
  R<C> rc; // class C is a friend of R<C>
519
- R<int> Ri; // OK: "friend int;" is ignored
520
  ```
521
 
522
  — *end example*]
523
 
524
  A function first declared in a friend declaration has the linkage of the
525
- namespace of which it is a member ([[basic.link]],
526
- [[namespace.memdef]]). Otherwise, the function retains its previous
527
- linkage [[dcl.stc]].
528
 
529
- When a friend declaration refers to an overloaded name or operator, only
530
- the function specified by the parameter types becomes a friend. A member
531
- function of a class `X` can be a friend of a class `Y`.
532
 
533
- [*Example 6*:
 
 
 
534
 
535
  ``` cpp
536
  class Y {
537
  friend char* X::foo(int);
538
  friend X::X(char); // constructors can be friends
@@ -540,15 +544,17 @@ class Y {
540
  };
541
  ```
542
 
543
  — *end example*]
544
 
545
- A function can be defined in a friend declaration of a class if and only
546
- if the class is a non-local class [[class.local]], the function name is
547
- unqualified, and the function has namespace scope.
548
 
549
- [*Example 7*:
 
 
 
 
550
 
551
  ``` cpp
552
  class M {
553
  friend void f() { } // definition of global f, a friend of M,
554
  // not the definition of a member function
@@ -556,26 +562,27 @@ class M {
556
  ```
557
 
558
  — *end example*]
559
 
560
  Such a function is implicitly an inline [[dcl.inline]] function if it is
561
- attached to the global module. A friend function defined in a class is
562
- in the (lexical) scope of the class in which it is defined. A friend
563
- function defined outside the class is not [[basic.lookup.unqual]].
 
564
 
565
  No *storage-class-specifier* shall appear in the *decl-specifier-seq* of
566
  a friend declaration.
567
 
568
- A name nominated by a friend declaration shall be accessible in the
569
- scope of the class containing the friend declaration. The meaning of the
570
- friend declaration is the same whether the friend declaration appears in
571
- the private, protected, or public [[class.mem]] portion of the class
572
  *member-specification*.
573
 
574
  Friendship is neither inherited nor transitive.
575
 
576
- [*Example 8*:
577
 
578
  ``` cpp
579
  class A {
580
  friend class B;
581
  int a;
@@ -598,19 +605,48 @@ class D : public B {
598
  };
599
  ```
600
 
601
  — *end example*]
602
 
603
- If a friend declaration appears in a local class [[class.local]] and the
604
- name specified is an unqualified name, a prior declaration is looked up
605
- without considering scopes that are outside the innermost enclosing
606
- non-class scope. For a friend function declaration, if there is no prior
607
- declaration, the program is ill-formed. For a friend class declaration,
608
- if there is no prior declaration, the class that is specified belongs to
609
- the innermost enclosing non-class scope, but if it is subsequently
610
- referenced, its name is not found by name lookup until a matching
611
- declaration is provided in the innermost enclosing non-class scope.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
612
 
613
  [*Example 9*:
614
 
615
  ``` cpp
616
  class X;
@@ -636,17 +672,19 @@ void f() {
636
  ### Protected member access <a id="class.protected">[[class.protected]]</a>
637
 
638
  An additional access check beyond those described earlier in
639
  [[class.access]] is applied when a non-static data member or non-static
640
  member function is a protected member of its naming class
641
- [[class.access.base]].[^13] As described earlier, access to a protected
642
- member is granted because the reference occurs in a friend or member of
643
- some class `C`. If the access is to form a pointer to member
644
- [[expr.unary.op]], the *nested-name-specifier* shall denote `C` or a
645
- class derived from `C`. All other accesses involve a (possibly implicit)
646
- object expression [[expr.ref]]. In this case, the class of the object
647
- expression shall be `C` or a class derived from `C`.
 
 
648
 
649
  [*Example 1*:
650
 
651
  ``` cpp
652
  class B {
@@ -716,11 +754,11 @@ private:
716
  void f() {
717
  D d;
718
  B* pb = &d;
719
  D* pd = &d;
720
 
721
- pb->f(); // OK: B::f() is public, D::f() is invoked
722
  pd->f(); // error: D::f() is private
723
  }
724
  ```
725
 
726
  — *end example*]
@@ -730,12 +768,13 @@ used to denote the object for which the member function is called (`B*`
730
  in the example above). The access of the member function in the class in
731
  which it was defined (`D` in the example above) is in general not known.
732
 
733
  ### Multiple access <a id="class.paths">[[class.paths]]</a>
734
 
735
- If a name can be reached by several paths through a multiple inheritance
736
- graph, the access is that of the path that gives most access.
 
737
 
738
  [*Example 1*:
739
 
740
  ``` cpp
741
  class W { public: void f(); };
@@ -764,14 +803,14 @@ shall be obeyed.
764
  class E {
765
  int x;
766
  class B { };
767
 
768
  class I {
769
- B b; // OK: E::I can access E::B
770
  int y;
771
  void f(E* p, int i) {
772
- p->x = i; // OK: E::I can access E::x
773
  }
774
  };
775
 
776
  int g(I* p) {
777
  return p->y; // error: I::y is private
 
1
  ## Member access control <a id="class.access">[[class.access]]</a>
2
 
3
+ ### General <a id="class.access.general">[[class.access.general]]</a>
4
+
5
  A member of a class can be
6
 
7
+ - private, that is, it can be named only by members and friends of the
8
+ class in which it is declared;
9
+ - protected, that is, it can be named only by members and friends of the
10
+ class in which it is declared, by classes derived from that class, and
11
+ by their friends (see  [[class.protected]]); or
12
+ - public, that is, it can be named anywhere without access restriction.
 
13
 
14
+ [*Note 1*: A constructor or destructor can be named by an expression
15
+ [[basic.def.odr]] even though it has no name. *end note*]
16
+
17
+ A member of a class can also access all the members to which the class
18
+ has access. A local class of a member function may access the same
19
+ members that the member function itself may access.[^11]
20
 
21
  Members of a class defined with the keyword `class` are `private` by
22
  default. Members of a class defined with the keywords `struct` or
23
  `union` are public by default.
24
 
 
34
  };
35
  ```
36
 
37
  — *end example*]
38
 
39
+ Access control is applied uniformly to declarations and expressions.
 
40
 
41
+ [*Note 2*: Access control applies to members nominated by friend
42
  declarations [[class.friend]] and *using-declaration*s
43
  [[namespace.udecl]]. — *end note*]
44
 
45
+ When a *using-declarator* is named, access control is applied to it, not
46
+ to the declarations that replace it. For an overload set, access control
47
+ is applied only to the function selected by overload resolution.
48
 
49
+ [*Example 2*:
50
 
51
+ ``` cpp
52
+ struct S {
53
+ void f(int);
54
+ private:
55
+ void f(double);
56
+ };
57
+
58
+ void g(S* sp) {
59
+ sp->f(2); // OK, access control applied after overload resolution
60
+ }
61
+ ```
62
+
63
+ — *end example*]
64
+
65
+ [*Note 3*:
66
+
67
+ Because access control applies to the declarations named, if access
68
+ control is applied to a *typedef-name*, only the accessibility of the
69
+ typedef or alias declaration itself is considered. The accessibility of
70
+ the entity referred to by the *typedef-name* is not considered. For
71
+ example,
72
 
73
  ``` cpp
74
  class A {
75
  class B { };
76
  public:
77
  typedef B BB;
78
  };
79
 
80
  void f() {
81
+ A::BB x; // OK, typedef A::BB is public
82
  A::B y; // access error, A::B is private
83
  }
84
  ```
85
 
86
  — *end note*]
87
 
88
+ [*Note 4*: Access control does not prevent members from being found by
89
+ name lookup or implicit conversions to base classes from being
90
+ considered. *end note*]
 
91
 
92
  The interpretation of a given construct is established without regard to
93
  access control. If the interpretation established makes use of
94
+ inaccessible members or base classes, the construct is ill-formed.
95
 
96
+ All access controls in [[class.access]] affect the ability to name a
97
+ class member from the declaration of a particular entity, including
98
  parts of the declaration preceding the name of the entity being declared
99
  and, if the entity is a class, the definitions of members of the class
100
  appearing outside the class’s *member-specification*.
101
 
102
+ [*Note 5*: This access also applies to implicit references to
103
  constructors, conversion functions, and destructors. — *end note*]
104
 
105
+ [*Example 3*:
106
 
107
  ``` cpp
108
  class A {
109
  typedef int I; // private member
110
  I f();
 
135
  `A`, so checking of *base-specifier*s must be deferred until the entire
136
  *base-specifier-list* has been seen.
137
 
138
  — *end example*]
139
 
140
+ Access is checked for a default argument [[dcl.fct.default]] at the
141
+ point of declaration, rather than at any points of use of the default
142
+ argument. Access checking for default arguments in function templates
143
+ and in member functions of class templates is performed as described in 
144
+ [[temp.inst]].
145
 
146
+ Access for a default *template-argument* [[temp.param]] is checked in
147
+ the context in which it appears rather than at any points of use of it.
 
148
 
149
+ [*Example 4*:
150
 
151
  ``` cpp
152
  class B { };
153
  template <class T> class C {
154
  protected:
 
163
 
164
  — *end example*]
165
 
166
  ### Access specifiers <a id="class.access.spec">[[class.access.spec]]</a>
167
 
168
+ Member declarations can be labeled by an *access-specifier*
169
+ [[class.derived]]:
170
 
171
  ``` bnf
172
  access-specifier ':' member-specificationₒₚₜ
173
  ```
174
 
 
206
  };
207
  ```
208
 
209
  — *end example*]
210
 
 
 
 
211
  When a member is redeclared within its class definition, the access
212
  specified at its redeclaration shall be the same as at its initial
213
  declaration.
214
 
215
  [*Example 3*:
 
224
  };
225
  ```
226
 
227
  — *end example*]
228
 
229
+ [*Note 1*: In a derived class, the lookup of a base class name will
230
  find the injected-class-name instead of the name of the base class in
231
  the scope in which it was declared. The injected-class-name might be
232
  less accessible than the name of the base class in the scope in which it
233
  was declared. — *end note*]
234
 
 
283
 
284
  — *end example*]
285
 
286
  [*Note 1*:
287
 
288
+ A member of a private base class can be inaccessible as inherited, but
289
+ accessible directly. Because of the rules on pointer conversions
290
+ [[conv.ptr]] and explicit casts
291
+ [[expr.type.conv]], [[expr.static.cast]], [[expr.cast]], a conversion
292
+ from a pointer to a derived class to a pointer to an inaccessible base
293
+ class can be ill-formed if an implicit conversion is used, but
294
+ well-formed if an explicit cast is used. For example,
295
 
296
  ``` cpp
297
  class B {
298
  public:
299
  int mi; // non-static member
 
312
  b.mi = 3; // OK (b.mi is different from this->mi)
313
  b.si = 3; // OK (b.si is different from this->si)
314
  ::B::si = 3; // OK
315
  ::B* bp1 = this; // error: B is a private base class
316
  ::B* bp2 = (::B*)this; // OK with cast
317
+ bp2->mi = 3; // OK, access through a pointer to B.
318
  }
319
  ```
320
 
321
  — *end note*]
322
 
323
  A base class `B` of `N` is *accessible* at *R*, if
324
 
325
  - an invented public member of `B` would be a public member of `N`, or
326
+ - *R* occurs in a direct member or friend of class `N`, and an invented
327
+ public member of `B` would be a private or protected member of `N`, or
328
+ - *R* occurs in a direct member or friend of a class `P` derived from
329
+ `N`, and an invented public member of `B` would be a private or
330
+ protected member of `P`, or
331
  - there exists a class `S` such that `B` is a base class of `S`
332
  accessible at *R* and `S` is a base class of `N` accessible at *R*.
333
 
334
  [*Example 2*:
335
 
 
353
  ```
354
 
355
  — *end example*]
356
 
357
  If a base class is accessible, one can implicitly convert a pointer to a
358
+ derived class to a pointer to that base class
359
+ [[conv.ptr]], [[conv.mem]].
360
 
361
  [*Note 2*: It follows that members and friends of a class `X` can
362
  implicitly convert an `X*` to a pointer to a private or protected
363
  immediate base class of `X`. — *end note*]
364
 
365
  The access to a member is affected by the class in which the member is
366
+ named. This naming class is the class in whose scope name lookup
367
+ performed a search that found the member.
368
 
369
  [*Note 3*: This class can be explicit, e.g., when a *qualified-id* is
370
  used, or implicit, e.g., when a class member access operator
371
  [[expr.ref]] is used (including cases where an implicit “`this->`” is
372
  added). If both a class member access operator and a *qualified-id* are
 
375
  *qualified-id* (that is, `T`). — *end note*]
376
 
377
  A member `m` is accessible at the point *R* when named in class `N` if
378
 
379
  - `m` as a member of `N` is public, or
380
+ - `m` as a member of `N` is private, and *R* occurs in a direct member
381
+ or friend of class `N`, or
382
+ - `m` as a member of `N` is protected, and *R* occurs in a direct member
383
+ or friend of class `N`, or in a member of a class `P` derived from
384
+ `N`, where `m` as a member of `P` is public, private, or protected, or
385
  - there exists a base class `B` of `N` that is accessible at *R*, and
386
  `m` is accessible at *R* when named in class `B`.
387
  \[*Example 3*:
388
  ``` cpp
389
  class B;
 
392
  int i;
393
  friend void f(B*);
394
  };
395
  class B : public A { };
396
  void f(B* p) {
397
+ p->i = 1; // OK, B* can be implicitly converted to A*, and f has access to i in A
398
  }
399
  ```
400
 
401
  — *end example*]
402
 
 
410
  member be accessible as named. — *end note*]
411
 
412
  ### Friends <a id="class.friend">[[class.friend]]</a>
413
 
414
  A friend of a class is a function or class that is given permission to
415
+ name the private and protected members of the class. A class specifies
416
+ its friends, if any, by way of friend declarations. Such declarations
417
+ give special access rights to the friends, but they do not make the
418
+ nominated friends members of the befriending class.
419
 
420
  [*Example 1*:
421
 
422
  The following example illustrates the differences between members and
423
  friends:
 
440
  }
441
  ```
442
 
443
  — *end example*]
444
 
445
+ Declaring a class to be a friend implies that private and protected
446
+ members of the class granting friendship can be named in the
447
+ *base-specifier*s and member declarations of the befriended class.
448
 
449
  [*Example 2*:
450
 
451
  ``` cpp
452
  class A {
453
  class B { };
454
  friend class X;
455
  };
456
 
457
+ struct X : A::B { // OK, A::B accessible to friend
458
+ A::B mx; // OK, A::B accessible to member of friend
459
  class Y {
460
+ A::B my; // OK, A::B accessible to nested member of friend
461
  };
462
  };
463
  ```
464
 
465
  — *end example*]
 
481
  };
482
  ```
483
 
484
  — *end example*]
485
 
 
 
 
 
 
 
 
 
 
 
 
 
486
  A friend declaration that does not declare a function shall have one of
487
  the following forms:
488
 
489
  ``` bnf
490
  friend elaborated-type-specifier ';'
491
  friend simple-type-specifier ';'
492
  friend typename-specifier ';'
493
  ```
494
 
495
+ [*Note 1*: A friend declaration can be the *declaration* in a
496
+ *template-declaration* [[temp.pre]], [[temp.friend]]. — *end note*]
497
 
498
  If the type specifier in a `friend` declaration designates a (possibly
499
  cv-qualified) class type, that class is declared as a friend; otherwise,
500
  the friend declaration is ignored.
501
 
502
+ [*Example 4*:
503
 
504
  ``` cpp
505
  class C;
506
  typedef C Ct;
507
 
508
  class X1 {
509
+ friend C; // OK, class C is a friend
510
  };
511
 
512
  class X2 {
513
+ friend Ct; // OK, class C is a friend
514
+ friend D; // error: D not found
515
+ friend class D; // OK, elaborated-type-specifier declares new class
516
  };
517
 
518
  template <typename T> class R {
519
  friend T;
520
  };
521
 
522
  R<C> rc; // class C is a friend of R<C>
523
+ R<int> Ri; // OK, "friend int;" is ignored
524
  ```
525
 
526
  — *end example*]
527
 
528
  A function first declared in a friend declaration has the linkage of the
529
+ namespace of which it is a member [[basic.link]]. Otherwise, the
530
+ function retains its previous linkage [[dcl.stc]].
 
531
 
532
+ [*Note 2*:
 
 
533
 
534
+ A friend declaration refers to an entity, not (all overloads of) a name.
535
+ A member function of a class `X` can be a friend of a class `Y`.
536
+
537
+ [*Example 5*:
538
 
539
  ``` cpp
540
  class Y {
541
  friend char* X::foo(int);
542
  friend X::X(char); // constructors can be friends
 
544
  };
545
  ```
546
 
547
  — *end example*]
548
 
549
+ *end note*]
 
 
550
 
551
+ A function may be defined in a friend declaration of a class if and only
552
+ if the class is a non-local class [[class.local]] and the function name
553
+ is unqualified.
554
+
555
+ [*Example 6*:
556
 
557
  ``` cpp
558
  class M {
559
  friend void f() { } // definition of global f, a friend of M,
560
  // not the definition of a member function
 
562
  ```
563
 
564
  — *end example*]
565
 
566
  Such a function is implicitly an inline [[dcl.inline]] function if it is
567
+ attached to the global module.
568
+
569
+ [*Note 3*: If a friend function is defined outside a class, it is not
570
+ in the scope of the class. — *end note*]
571
 
572
  No *storage-class-specifier* shall appear in the *decl-specifier-seq* of
573
  a friend declaration.
574
 
575
+ A member nominated by a friend declaration shall be accessible in the
576
+ class containing the friend declaration. The meaning of the friend
577
+ declaration is the same whether the friend declaration appears in the
578
+ private, protected, or public [[class.mem]] portion of the class
579
  *member-specification*.
580
 
581
  Friendship is neither inherited nor transitive.
582
 
583
+ [*Example 7*:
584
 
585
  ``` cpp
586
  class A {
587
  friend class B;
588
  int a;
 
605
  };
606
  ```
607
 
608
  — *end example*]
609
 
610
+ [*Note 4*: A friend declaration never binds any names
611
+ [[dcl.meaning]], [[dcl.type.elab]]. *end note*]
612
+
613
+ [*Example 8*:
614
+
615
+ ``` cpp
616
+ // Assume f and g have not yet been declared.
617
+ void h(int);
618
+ template <class T> void f2(T);
619
+ namespace A {
620
+ class X {
621
+ friend void f(X); // A::f(X) is a friend
622
+ class Y {
623
+ friend void g(); // A::g is a friend
624
+ friend void h(int); // A::h is a friend
625
+ // ::h not considered
626
+ friend void f2<>(int); // ::f2<>(int) is a friend
627
+ };
628
+ };
629
+
630
+ // A::f, A::g and A::h are not visible here
631
+ X x;
632
+ void g() { f(x); } // definition of A::g
633
+ void f(X) { ... } // definition of A::f
634
+ void h(int) { ... } // definition of A::h
635
+ // A::f, A::g and A::h are visible here and known to be friends
636
+ }
637
+
638
+ using A::x;
639
+
640
+ void h() {
641
+ A::f(x);
642
+ A::X::f(x); // error: f is not a member of A::X
643
+ A::X::Y::g(); // error: g is not a member of A::X::Y
644
+ }
645
+ ```
646
+
647
+ — *end example*]
648
 
649
  [*Example 9*:
650
 
651
  ``` cpp
652
  class X;
 
672
  ### Protected member access <a id="class.protected">[[class.protected]]</a>
673
 
674
  An additional access check beyond those described earlier in
675
  [[class.access]] is applied when a non-static data member or non-static
676
  member function is a protected member of its naming class
677
+ [[class.access.base]].[^13]
678
+
679
+ As described earlier, access to a protected member is granted because
680
+ the reference occurs in a friend or direct member of some class `C`. If
681
+ the access is to form a pointer to member [[expr.unary.op]], the
682
+ *nested-name-specifier* shall denote `C` or a class derived from `C`.
683
+ All other accesses involve a (possibly implicit) object expression
684
+ [[expr.ref]]. In this case, the class of the object expression shall be
685
+ `C` or a class derived from `C`.
686
 
687
  [*Example 1*:
688
 
689
  ``` cpp
690
  class B {
 
754
  void f() {
755
  D d;
756
  B* pb = &d;
757
  D* pd = &d;
758
 
759
+ pb->f(); // OK, B::f() is public, D::f() is invoked
760
  pd->f(); // error: D::f() is private
761
  }
762
  ```
763
 
764
  — *end example*]
 
768
  in the example above). The access of the member function in the class in
769
  which it was defined (`D` in the example above) is in general not known.
770
 
771
  ### Multiple access <a id="class.paths">[[class.paths]]</a>
772
 
773
+ If a declaration can be reached by several paths through a multiple
774
+ inheritance graph, the access is that of the path that gives most
775
+ access.
776
 
777
  [*Example 1*:
778
 
779
  ``` cpp
780
  class W { public: void f(); };
 
803
  class E {
804
  int x;
805
  class B { };
806
 
807
  class I {
808
+ B b; // OK, E::I can access E::B
809
  int y;
810
  void f(E* p, int i) {
811
+ p->x = i; // OK, E::I can access E::x
812
  }
813
  };
814
 
815
  int g(I* p) {
816
  return p->y; // error: I::y is private