From Jason Turner

[temp.friend]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpkijoimgi/{from.md → to.md} +29 -14
tmp/tmpkijoimgi/{from.md → to.md} RENAMED
@@ -16,10 +16,12 @@ declaration that is not a template declaration:
16
  declaration refers to the deduced specialization of that function
17
  template ([[temp.deduct.decl]]), otherwise,
18
  - the name shall be an *unqualified-id* that declares (or redeclares) a
19
  non-template function.
20
 
 
 
21
  ``` cpp
22
  template<class T> class task;
23
  template<class T> task<T>* preempt(task<T>*);
24
 
25
  template<class T> class task {
@@ -44,54 +46,61 @@ function template `preempt` as a friend; and each specialization of the
44
  `task` class template has all specializations of the function template
45
  `func` as friends. Similarly, each specialization of the `task` class
46
  template has the class template specialization `task<int>` as a friend,
47
  and has all specializations of the class template `frd` as friends.
48
 
 
 
49
  A friend template may be declared within a class or class template. A
50
  friend function template may be defined within a class or class
51
  template, but a friend class template may not be defined in a class or
52
  class template. In these cases, all specializations of the friend class
53
  or friend function template are friends of the class or class template
54
  granting friendship.
55
 
 
 
56
  ``` cpp
57
  class A {
58
  template<class T> friend class B; // OK
59
- template<class T> friend void f(T){ /* ... */ } // OK
60
  };
61
  ```
62
 
 
 
63
  A template friend declaration specifies that all specializations of that
64
  template, whether they are implicitly instantiated ([[temp.inst]]),
65
  partially specialized ([[temp.class.spec]]) or explicitly specialized (
66
  [[temp.expl.spec]]), are friends of the class containing the template
67
  friend declaration.
68
 
 
 
69
  ``` cpp
70
  class X {
71
  template<class T> friend struct A;
72
  class Y { };
73
  };
74
 
75
  template<class T> struct A { X::Y ab; }; // OK
76
  template<class T> struct A<T*> { X::Y ab; }; // OK
77
  ```
78
 
79
- When a function is defined in a friend function declaration in a class
80
- template, the function is instantiated when the function is odr-used (
81
- [[basic.def.odr]]). The same restrictions on multiple declarations and
82
- definitions that apply to non-template function declarations and
83
- definitions also apply to these implicit definitions.
84
 
85
  A member of a class template may be declared to be a friend of a
86
  non-template class. In this case, the corresponding member of every
87
- specialization of the class template is a friend of the class granting
88
- friendship. For explicit specializations the corresponding member is the
89
- member (if any) that has the same name, kind (type, function, class
90
- template, or function template), template parameters, and signature as
91
- the member of the class template instantiation that would otherwise have
92
- been generated.
 
 
 
93
 
94
  ``` cpp
95
  template<class T> struct A {
96
  struct B { };
97
  void f();
@@ -115,23 +124,29 @@ class C {
115
  template<class T> friend void A<T>::D::g(); // does not grant friendship to A<int>::D::g()
116
  // because A<int>::D is not a specialization of A<T>::D
117
  };
118
  ```
119
 
120
- A friend declaration may first declare a member of an enclosing
121
- namespace scope ([[temp.inject]]).
 
 
122
 
123
  A friend template shall not be declared in a local class.
124
 
125
  Friend declarations shall not declare partial specializations.
126
 
 
 
127
  ``` cpp
128
  template<class T> class A { };
129
  class X {
130
  template<class T> friend class A<T*>; // error
131
  };
132
  ```
133
 
 
 
134
  When a friend declaration refers to a specialization of a function
135
  template, the function parameter declarations shall not include default
136
  arguments, nor shall the inline specifier be used in such a declaration.
137
 
 
16
  declaration refers to the deduced specialization of that function
17
  template ([[temp.deduct.decl]]), otherwise,
18
  - the name shall be an *unqualified-id* that declares (or redeclares) a
19
  non-template function.
20
 
21
+ [*Example 1*:
22
+
23
  ``` cpp
24
  template<class T> class task;
25
  template<class T> task<T>* preempt(task<T>*);
26
 
27
  template<class T> class task {
 
46
  `task` class template has all specializations of the function template
47
  `func` as friends. Similarly, each specialization of the `task` class
48
  template has the class template specialization `task<int>` as a friend,
49
  and has all specializations of the class template `frd` as friends.
50
 
51
+ — *end example*]
52
+
53
  A friend template may be declared within a class or class template. A
54
  friend function template may be defined within a class or class
55
  template, but a friend class template may not be defined in a class or
56
  class template. In these cases, all specializations of the friend class
57
  or friend function template are friends of the class or class template
58
  granting friendship.
59
 
60
+ [*Example 2*:
61
+
62
  ``` cpp
63
  class A {
64
  template<class T> friend class B; // OK
65
+ template<class T> friend void f(T){ ... } // OK
66
  };
67
  ```
68
 
69
+ — *end example*]
70
+
71
  A template friend declaration specifies that all specializations of that
72
  template, whether they are implicitly instantiated ([[temp.inst]]),
73
  partially specialized ([[temp.class.spec]]) or explicitly specialized (
74
  [[temp.expl.spec]]), are friends of the class containing the template
75
  friend declaration.
76
 
77
+ [*Example 3*:
78
+
79
  ``` cpp
80
  class X {
81
  template<class T> friend struct A;
82
  class Y { };
83
  };
84
 
85
  template<class T> struct A { X::Y ab; }; // OK
86
  template<class T> struct A<T*> { X::Y ab; }; // OK
87
  ```
88
 
89
+ *end example*]
 
 
 
 
90
 
91
  A member of a class template may be declared to be a friend of a
92
  non-template class. In this case, the corresponding member of every
93
+ specialization of the primary class template and class template partial
94
+ specializations thereof is a friend of the class granting friendship.
95
+ For explicit specializations and specializations of partial
96
+ specializations, the corresponding member is the member (if any) that
97
+ has the same name, kind (type, function, class template, or function
98
+ template), template parameters, and signature as the member of the class
99
+ template instantiation that would otherwise have been generated.
100
+
101
+ [*Example 4*:
102
 
103
  ``` cpp
104
  template<class T> struct A {
105
  struct B { };
106
  void f();
 
124
  template<class T> friend void A<T>::D::g(); // does not grant friendship to A<int>::D::g()
125
  // because A<int>::D is not a specialization of A<T>::D
126
  };
127
  ```
128
 
129
+ *end example*]
130
+
131
+ [*Note 1*: A friend declaration may first declare a member of an
132
+ enclosing namespace scope ([[temp.inject]]). — *end note*]
133
 
134
  A friend template shall not be declared in a local class.
135
 
136
  Friend declarations shall not declare partial specializations.
137
 
138
+ [*Example 5*:
139
+
140
  ``` cpp
141
  template<class T> class A { };
142
  class X {
143
  template<class T> friend class A<T*>; // error
144
  };
145
  ```
146
 
147
+ — *end example*]
148
+
149
  When a friend declaration refers to a specialization of a function
150
  template, the function parameter declarations shall not include default
151
  arguments, nor shall the inline specifier be used in such a declaration.
152