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 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 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
|
| 35 |
-
|
| 36 |
-
|
| 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
|
| 47 |
-
A::B mx; // OK
|
| 48 |
class Y {
|
| 49 |
-
A::B my; // OK
|
| 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
|
| 97 |
-
*template-declaration*
|
| 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
|
| 104 |
|
| 105 |
``` cpp
|
| 106 |
class C;
|
| 107 |
typedef C Ct;
|
| 108 |
|
| 109 |
class X1 {
|
| 110 |
-
friend C; // OK
|
| 111 |
};
|
| 112 |
|
| 113 |
class X2 {
|
| 114 |
-
friend Ct; // OK
|
| 115 |
-
friend D; // error:
|
| 116 |
-
friend class D; // OK
|
| 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
|
| 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
|
| 131 |
-
|
| 132 |
-
linkage [[dcl.stc]].
|
| 133 |
|
| 134 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 167 |
-
|
| 168 |
-
function defined outside
|
|
|
|
| 169 |
|
| 170 |
No *storage-class-specifier* shall appear in the *decl-specifier-seq* of
|
| 171 |
a friend declaration.
|
| 172 |
|
| 173 |
-
A
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
*member-specification*.
|
| 178 |
|
| 179 |
Friendship is neither inherited nor transitive.
|
| 180 |
|
| 181 |
-
[*Example
|
| 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 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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;
|