From Jason Turner

[class.friend]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpsc9q05bs/{from.md → to.md} +78 -58
tmp/tmpsc9q05bs/{from.md → to.md} RENAMED
@@ -1,12 +1,12 @@
1
  ### Friends <a id="class.friend">[[class.friend]]</a>
2
 
3
  A friend of a class is a function or class that is given permission to
4
- use the private and protected member names from the class. A class
5
- specifies its friends, if any, by way of friend declarations. Such
6
- declarations give special access rights to the friends, but they do not
7
- make the nominated friends members of the befriending class.
8
 
9
  [*Example 1*:
10
 
11
  The following example illustrates the differences between members and
12
  friends:
@@ -29,26 +29,26 @@ void f() {
29
  }
30
  ```
31
 
32
  — *end example*]
33
 
34
- Declaring a class to be a friend implies that the names of private and
35
- protected members from the class granting friendship can be accessed in
36
- the *base-specifier*s and member declarations of the befriended class.
37
 
38
  [*Example 2*:
39
 
40
  ``` cpp
41
  class A {
42
  class B { };
43
  friend class X;
44
  };
45
 
46
- struct X : A::B { // OK: A::B accessible to friend
47
- A::B mx; // OK: A::B accessible to member of friend
48
  class Y {
49
- A::B my; // OK: A::B accessible to nested member of friend
50
  };
51
  };
52
  ```
53
 
54
  — *end example*]
@@ -70,74 +70,62 @@ class Z {
70
  };
71
  ```
72
 
73
  — *end example*]
74
 
75
- A class shall not be defined in a friend declaration.
76
-
77
- [*Example 4*:
78
-
79
- ``` cpp
80
- class A {
81
- friend class B { }; // error: cannot define class in friend declaration
82
- };
83
- ```
84
-
85
- — *end example*]
86
-
87
  A friend declaration that does not declare a function shall have one of
88
  the following forms:
89
 
90
  ``` bnf
91
  friend elaborated-type-specifier ';'
92
  friend simple-type-specifier ';'
93
  friend typename-specifier ';'
94
  ```
95
 
96
- [*Note 1*: A friend declaration may be the *declaration* in a
97
- *template-declaration* ([[temp.pre]], [[temp.friend]]). — *end note*]
98
 
99
  If the type specifier in a `friend` declaration designates a (possibly
100
  cv-qualified) class type, that class is declared as a friend; otherwise,
101
  the friend declaration is ignored.
102
 
103
- [*Example 5*:
104
 
105
  ``` cpp
106
  class C;
107
  typedef C Ct;
108
 
109
  class X1 {
110
- friend C; // OK: class C is a friend
111
  };
112
 
113
  class X2 {
114
- friend Ct; // OK: class C is a friend
115
- friend D; // error: no type-name D in scope
116
- friend class D; // OK: elaborated-type-specifier declares new class
117
  };
118
 
119
  template <typename T> class R {
120
  friend T;
121
  };
122
 
123
  R<C> rc; // class C is a friend of R<C>
124
- R<int> Ri; // OK: "friend int;" is ignored
125
  ```
126
 
127
  — *end example*]
128
 
129
  A function first declared in a friend declaration has the linkage of the
130
- namespace of which it is a member ([[basic.link]],
131
- [[namespace.memdef]]). Otherwise, the function retains its previous
132
- linkage [[dcl.stc]].
133
 
134
- When a friend declaration refers to an overloaded name or operator, only
135
- the function specified by the parameter types becomes a friend. A member
136
- function of a class `X` can be a friend of a class `Y`.
137
 
138
- [*Example 6*:
 
 
 
139
 
140
  ``` cpp
141
  class Y {
142
  friend char* X::foo(int);
143
  friend X::X(char); // constructors can be friends
@@ -145,15 +133,17 @@ class Y {
145
  };
146
  ```
147
 
148
  — *end example*]
149
 
150
- A function can be defined in a friend declaration of a class if and only
151
- if the class is a non-local class [[class.local]], the function name is
152
- unqualified, and the function has namespace scope.
153
 
154
- [*Example 7*:
 
 
 
 
155
 
156
  ``` cpp
157
  class M {
158
  friend void f() { } // definition of global f, a friend of M,
159
  // not the definition of a member function
@@ -161,26 +151,27 @@ class M {
161
  ```
162
 
163
  — *end example*]
164
 
165
  Such a function is implicitly an inline [[dcl.inline]] function if it is
166
- attached to the global module. A friend function defined in a class is
167
- in the (lexical) scope of the class in which it is defined. A friend
168
- function defined outside the class is not [[basic.lookup.unqual]].
 
169
 
170
  No *storage-class-specifier* shall appear in the *decl-specifier-seq* of
171
  a friend declaration.
172
 
173
- A name nominated by a friend declaration shall be accessible in the
174
- scope of the class containing the friend declaration. The meaning of the
175
- friend declaration is the same whether the friend declaration appears in
176
- the private, protected, or public [[class.mem]] portion of the class
177
  *member-specification*.
178
 
179
  Friendship is neither inherited nor transitive.
180
 
181
- [*Example 8*:
182
 
183
  ``` cpp
184
  class A {
185
  friend class B;
186
  int a;
@@ -203,19 +194,48 @@ class D : public B {
203
  };
204
  ```
205
 
206
  — *end example*]
207
 
208
- If a friend declaration appears in a local class [[class.local]] and the
209
- name specified is an unqualified name, a prior declaration is looked up
210
- without considering scopes that are outside the innermost enclosing
211
- non-class scope. For a friend function declaration, if there is no prior
212
- declaration, the program is ill-formed. For a friend class declaration,
213
- if there is no prior declaration, the class that is specified belongs to
214
- the innermost enclosing non-class scope, but if it is subsequently
215
- referenced, its name is not found by name lookup until a matching
216
- declaration is provided in the innermost enclosing non-class scope.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
 
218
  [*Example 9*:
219
 
220
  ``` cpp
221
  class X;
 
1
  ### Friends <a id="class.friend">[[class.friend]]</a>
2
 
3
  A friend of a class is a function or class that is given permission to
4
+ name the private and protected members of the class. A class specifies
5
+ its friends, if any, by way of friend declarations. Such declarations
6
+ give special access rights to the friends, but they do not make the
7
+ nominated friends members of the befriending class.
8
 
9
  [*Example 1*:
10
 
11
  The following example illustrates the differences between members and
12
  friends:
 
29
  }
30
  ```
31
 
32
  — *end example*]
33
 
34
+ Declaring a class to be a friend implies that private and protected
35
+ members of the class granting friendship can be named in the
36
+ *base-specifier*s and member declarations of the befriended class.
37
 
38
  [*Example 2*:
39
 
40
  ``` cpp
41
  class A {
42
  class B { };
43
  friend class X;
44
  };
45
 
46
+ struct X : A::B { // OK, A::B accessible to friend
47
+ A::B mx; // OK, A::B accessible to member of friend
48
  class Y {
49
+ A::B my; // OK, A::B accessible to nested member of friend
50
  };
51
  };
52
  ```
53
 
54
  — *end example*]
 
70
  };
71
  ```
72
 
73
  — *end example*]
74
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  A friend declaration that does not declare a function shall have one of
76
  the following forms:
77
 
78
  ``` bnf
79
  friend elaborated-type-specifier ';'
80
  friend simple-type-specifier ';'
81
  friend typename-specifier ';'
82
  ```
83
 
84
+ [*Note 1*: A friend declaration can be the *declaration* in a
85
+ *template-declaration* [[temp.pre]], [[temp.friend]]. — *end note*]
86
 
87
  If the type specifier in a `friend` declaration designates a (possibly
88
  cv-qualified) class type, that class is declared as a friend; otherwise,
89
  the friend declaration is ignored.
90
 
91
+ [*Example 4*:
92
 
93
  ``` cpp
94
  class C;
95
  typedef C Ct;
96
 
97
  class X1 {
98
+ friend C; // OK, class C is a friend
99
  };
100
 
101
  class X2 {
102
+ friend Ct; // OK, class C is a friend
103
+ friend D; // error: D not found
104
+ friend class D; // OK, elaborated-type-specifier declares new class
105
  };
106
 
107
  template <typename T> class R {
108
  friend T;
109
  };
110
 
111
  R<C> rc; // class C is a friend of R<C>
112
+ R<int> Ri; // OK, "friend int;" is ignored
113
  ```
114
 
115
  — *end example*]
116
 
117
  A function first declared in a friend declaration has the linkage of the
118
+ namespace of which it is a member [[basic.link]]. Otherwise, the
119
+ function retains its previous linkage [[dcl.stc]].
 
120
 
121
+ [*Note 2*:
 
 
122
 
123
+ A friend declaration refers to an entity, not (all overloads of) a name.
124
+ A member function of a class `X` can be a friend of a class `Y`.
125
+
126
+ [*Example 5*:
127
 
128
  ``` cpp
129
  class Y {
130
  friend char* X::foo(int);
131
  friend X::X(char); // constructors can be friends
 
133
  };
134
  ```
135
 
136
  — *end example*]
137
 
138
+ *end note*]
 
 
139
 
140
+ A function may be defined in a friend declaration of a class if and only
141
+ if the class is a non-local class [[class.local]] and the function name
142
+ is unqualified.
143
+
144
+ [*Example 6*:
145
 
146
  ``` cpp
147
  class M {
148
  friend void f() { } // definition of global f, a friend of M,
149
  // not the definition of a member function
 
151
  ```
152
 
153
  — *end example*]
154
 
155
  Such a function is implicitly an inline [[dcl.inline]] function if it is
156
+ attached to the global module.
157
+
158
+ [*Note 3*: If a friend function is defined outside a class, it is not
159
+ in the scope of the class. — *end note*]
160
 
161
  No *storage-class-specifier* shall appear in the *decl-specifier-seq* of
162
  a friend declaration.
163
 
164
+ A member nominated by a friend declaration shall be accessible in the
165
+ class containing the friend declaration. The meaning of the friend
166
+ declaration is the same whether the friend declaration appears in the
167
+ private, protected, or public [[class.mem]] portion of the class
168
  *member-specification*.
169
 
170
  Friendship is neither inherited nor transitive.
171
 
172
+ [*Example 7*:
173
 
174
  ``` cpp
175
  class A {
176
  friend class B;
177
  int a;
 
194
  };
195
  ```
196
 
197
  — *end example*]
198
 
199
+ [*Note 4*: A friend declaration never binds any names
200
+ [[dcl.meaning]], [[dcl.type.elab]]. *end note*]
201
+
202
+ [*Example 8*:
203
+
204
+ ``` cpp
205
+ // Assume f and g have not yet been declared.
206
+ void h(int);
207
+ template <class T> void f2(T);
208
+ namespace A {
209
+ class X {
210
+ friend void f(X); // A::f(X) is a friend
211
+ class Y {
212
+ friend void g(); // A::g is a friend
213
+ friend void h(int); // A::h is a friend
214
+ // ::h not considered
215
+ friend void f2<>(int); // ::f2<>(int) is a friend
216
+ };
217
+ };
218
+
219
+ // A::f, A::g and A::h are not visible here
220
+ X x;
221
+ void g() { f(x); } // definition of A::g
222
+ void f(X) { ... } // definition of A::f
223
+ void h(int) { ... } // definition of A::h
224
+ // A::f, A::g and A::h are visible here and known to be friends
225
+ }
226
+
227
+ using A::x;
228
+
229
+ void h() {
230
+ A::f(x);
231
+ A::X::f(x); // error: f is not a member of A::X
232
+ A::X::Y::g(); // error: g is not a member of A::X::Y
233
+ }
234
+ ```
235
+
236
+ — *end example*]
237
 
238
  [*Example 9*:
239
 
240
  ``` cpp
241
  class X;