tmp/tmp4ms8fnt1/{from.md → to.md}
RENAMED
|
@@ -1,25 +1,29 @@
|
|
| 1 |
-
## Virtual functions <a id="class.virtual">[[class.virtual]]</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
[*Note 1*: Virtual functions support dynamic binding and
|
| 4 |
object-oriented programming. — *end note*]
|
| 5 |
|
| 6 |
A class that declares or inherits a virtual function is called a
|
| 7 |
-
*polymorphic class*.
|
| 8 |
|
| 9 |
If a virtual member function `vf` is declared in a class `Base` and in a
|
| 10 |
class `Derived`, derived directly or indirectly from `Base`, a member
|
| 11 |
-
function `vf` with the same name, parameter-type-list
|
| 12 |
cv-qualification, and ref-qualifier (or absence of same) as `Base::vf`
|
| 13 |
-
is declared, then `Derived::vf`
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
class
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
[*Example 1*:
|
| 23 |
|
| 24 |
``` cpp
|
| 25 |
struct A {
|
|
@@ -113,10 +117,23 @@ struct D : B {
|
|
| 113 |
};
|
| 114 |
```
|
| 115 |
|
| 116 |
— *end example*]
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
Even though destructors are not inherited, a destructor in a derived
|
| 119 |
class overrides a base class destructor declared virtual; see
|
| 120 |
[[class.dtor]] and [[class.free]].
|
| 121 |
|
| 122 |
The return type of an overriding function shall be either identical to
|
|
@@ -124,11 +141,11 @@ the return type of the overridden function or *covariant* with the
|
|
| 124 |
classes of the functions. If a function `D::f` overrides a function
|
| 125 |
`B::f`, the return types of the functions are covariant if they satisfy
|
| 126 |
the following criteria:
|
| 127 |
|
| 128 |
- both are pointers to classes, both are lvalue references to classes,
|
| 129 |
-
or both are rvalue references to classes[^
|
| 130 |
- the class in the return type of `B::f` is the same class as the class
|
| 131 |
in the return type of `D::f`, or is an unambiguous and accessible
|
| 132 |
direct or indirect base class of the class in the return type of
|
| 133 |
`D::f`
|
| 134 |
- both pointers or references have the same cv-qualification and the
|
|
@@ -139,13 +156,13 @@ the following criteria:
|
|
| 139 |
If the class type in the covariant return type of `D::f` differs from
|
| 140 |
that of `B::f`, the class type in the return type of `D::f` shall be
|
| 141 |
complete at the point of declaration of `D::f` or shall be the class
|
| 142 |
type `D`. When the overriding function is called as the final overrider
|
| 143 |
of the overridden function, its result is converted to the type returned
|
| 144 |
-
by the (statically chosen) overridden function
|
| 145 |
|
| 146 |
-
[*Example
|
| 147 |
|
| 148 |
``` cpp
|
| 149 |
class B { };
|
| 150 |
class D : private B { friend class Derived; };
|
| 151 |
struct Base {
|
|
@@ -176,39 +193,39 @@ void g() {
|
|
| 176 |
Base* bp = &d; // standard conversion:
|
| 177 |
// Derived* to Base*
|
| 178 |
bp->vf1(); // calls Derived::vf1()
|
| 179 |
bp->vf2(); // calls Base::vf2()
|
| 180 |
bp->f(); // calls Base::f() (not virtual)
|
| 181 |
-
B* p = bp->vf4(); // calls Derived::
|
| 182 |
// result to B*
|
| 183 |
Derived* dp = &d;
|
| 184 |
-
D* q = dp->vf4(); // calls Derived::
|
| 185 |
// convert the result to B*
|
| 186 |
-
dp->vf2(); //
|
| 187 |
}
|
| 188 |
```
|
| 189 |
|
| 190 |
— *end example*]
|
| 191 |
|
| 192 |
[*Note 3*: The interpretation of the call of a virtual function depends
|
| 193 |
on the type of the object for which it is called (the dynamic type),
|
| 194 |
whereas the interpretation of a call of a non-virtual member function
|
| 195 |
depends only on the type of the pointer or reference denoting that
|
| 196 |
-
object (the static type)
|
| 197 |
|
| 198 |
[*Note 4*: The `virtual` specifier implies membership, so a virtual
|
| 199 |
-
function cannot be a non-member
|
| 200 |
virtual function be a static member, since a virtual function call
|
| 201 |
relies on a specific object for determining which function to invoke. A
|
| 202 |
-
virtual function declared in one class can be declared a
|
| 203 |
-
another class. — *end note*]
|
| 204 |
|
| 205 |
A virtual function declared in a class shall be defined, or declared
|
| 206 |
-
pure
|
| 207 |
-
required
|
| 208 |
|
| 209 |
-
[*Example
|
| 210 |
|
| 211 |
Here are some uses of virtual functions with multiple base classes:
|
| 212 |
|
| 213 |
``` cpp
|
| 214 |
struct A {
|
|
@@ -231,22 +248,22 @@ void foo() {
|
|
| 231 |
// A* ap = &d; // would be ill-formed: ambiguous
|
| 232 |
B1* b1p = &d;
|
| 233 |
A* ap = b1p;
|
| 234 |
D* dp = &d;
|
| 235 |
ap->f(); // calls D::B1::f
|
| 236 |
-
dp->f(); //
|
| 237 |
}
|
| 238 |
```
|
| 239 |
|
| 240 |
In class `D` above there are two occurrences of class `A` and hence two
|
| 241 |
occurrences of the virtual member function `A::f`. The final overrider
|
| 242 |
of `B1::A::f` is `B1::f` and the final overrider of `B2::A::f` is
|
| 243 |
`B2::f`.
|
| 244 |
|
| 245 |
— *end example*]
|
| 246 |
|
| 247 |
-
[*Example
|
| 248 |
|
| 249 |
The following example shows a function that does not have a unique final
|
| 250 |
overrider:
|
| 251 |
|
| 252 |
``` cpp
|
|
@@ -260,26 +277,26 @@ struct VB1 : virtual A { // note virtual derivation
|
|
| 260 |
|
| 261 |
struct VB2 : virtual A {
|
| 262 |
void f();
|
| 263 |
};
|
| 264 |
|
| 265 |
-
struct Error : VB1, VB2 { //
|
| 266 |
};
|
| 267 |
|
| 268 |
struct Okay : VB1, VB2 {
|
| 269 |
void f();
|
| 270 |
};
|
| 271 |
```
|
| 272 |
|
| 273 |
Both `VB1::f` and `VB2::f` override `A::f` but there is no overrider of
|
| 274 |
both of them in class `Error`. This example is therefore ill-formed.
|
| 275 |
-
Class `Okay` is well
|
| 276 |
overrider.
|
| 277 |
|
| 278 |
— *end example*]
|
| 279 |
|
| 280 |
-
[*Example
|
| 281 |
|
| 282 |
The following example uses the well-formed classes from above.
|
| 283 |
|
| 284 |
``` cpp
|
| 285 |
struct VB1a : virtual A { // does not declare f
|
|
@@ -294,14 +311,14 @@ void foe() {
|
|
| 294 |
}
|
| 295 |
```
|
| 296 |
|
| 297 |
— *end example*]
|
| 298 |
|
| 299 |
-
Explicit qualification with the scope operator
|
| 300 |
suppresses the virtual call mechanism.
|
| 301 |
|
| 302 |
-
[*Example
|
| 303 |
|
| 304 |
``` cpp
|
| 305 |
class B { public: virtual void f(); };
|
| 306 |
class D : public B { public: void f(); };
|
| 307 |
|
|
@@ -311,10 +328,14 @@ void D::f() { ... B::f(); }
|
|
| 311 |
Here, the function call in `D::f` really does call `B::f` and not
|
| 312 |
`D::f`.
|
| 313 |
|
| 314 |
— *end example*]
|
| 315 |
|
| 316 |
-
A function with a deleted definition
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
|
|
|
| 1 |
+
### Virtual functions <a id="class.virtual">[[class.virtual]]</a>
|
| 2 |
+
|
| 3 |
+
A non-static member function is a *virtual function* if it is first
|
| 4 |
+
declared with the keyword `virtual` or if it overrides a virtual member
|
| 5 |
+
function declared in a base class (see below).[^7]
|
| 6 |
|
| 7 |
[*Note 1*: Virtual functions support dynamic binding and
|
| 8 |
object-oriented programming. — *end note*]
|
| 9 |
|
| 10 |
A class that declares or inherits a virtual function is called a
|
| 11 |
+
*polymorphic class*.[^8]
|
| 12 |
|
| 13 |
If a virtual member function `vf` is declared in a class `Base` and in a
|
| 14 |
class `Derived`, derived directly or indirectly from `Base`, a member
|
| 15 |
+
function `vf` with the same name, parameter-type-list [[dcl.fct]],
|
| 16 |
cv-qualification, and ref-qualifier (or absence of same) as `Base::vf`
|
| 17 |
+
is declared, then `Derived::vf` *overrides*[^9] `Base::vf`. For
|
| 18 |
+
convenience we say that any virtual function overrides itself. A virtual
|
| 19 |
+
member function `C::vf` of a class object `S` is a *final overrider*
|
| 20 |
+
unless the most derived class [[intro.object]] of which `S` is a base
|
| 21 |
+
class subobject (if any) declares or inherits another member function
|
| 22 |
+
that overrides `vf`. In a derived class, if a virtual member function of
|
| 23 |
+
a base class subobject has more than one final overrider the program is
|
| 24 |
+
ill-formed.
|
| 25 |
|
| 26 |
[*Example 1*:
|
| 27 |
|
| 28 |
``` cpp
|
| 29 |
struct A {
|
|
|
|
| 117 |
};
|
| 118 |
```
|
| 119 |
|
| 120 |
— *end example*]
|
| 121 |
|
| 122 |
+
A virtual function shall not have a trailing *requires-clause*
|
| 123 |
+
[[dcl.decl]].
|
| 124 |
+
|
| 125 |
+
[*Example 5*:
|
| 126 |
+
|
| 127 |
+
``` cpp
|
| 128 |
+
struct A {
|
| 129 |
+
virtual void f() requires true; // error: virtual function cannot be constrained[temp.constr.decl]
|
| 130 |
+
};
|
| 131 |
+
```
|
| 132 |
+
|
| 133 |
+
— *end example*]
|
| 134 |
+
|
| 135 |
Even though destructors are not inherited, a destructor in a derived
|
| 136 |
class overrides a base class destructor declared virtual; see
|
| 137 |
[[class.dtor]] and [[class.free]].
|
| 138 |
|
| 139 |
The return type of an overriding function shall be either identical to
|
|
|
|
| 141 |
classes of the functions. If a function `D::f` overrides a function
|
| 142 |
`B::f`, the return types of the functions are covariant if they satisfy
|
| 143 |
the following criteria:
|
| 144 |
|
| 145 |
- both are pointers to classes, both are lvalue references to classes,
|
| 146 |
+
or both are rvalue references to classes[^10]
|
| 147 |
- the class in the return type of `B::f` is the same class as the class
|
| 148 |
in the return type of `D::f`, or is an unambiguous and accessible
|
| 149 |
direct or indirect base class of the class in the return type of
|
| 150 |
`D::f`
|
| 151 |
- both pointers or references have the same cv-qualification and the
|
|
|
|
| 156 |
If the class type in the covariant return type of `D::f` differs from
|
| 157 |
that of `B::f`, the class type in the return type of `D::f` shall be
|
| 158 |
complete at the point of declaration of `D::f` or shall be the class
|
| 159 |
type `D`. When the overriding function is called as the final overrider
|
| 160 |
of the overridden function, its result is converted to the type returned
|
| 161 |
+
by the (statically chosen) overridden function [[expr.call]].
|
| 162 |
|
| 163 |
+
[*Example 6*:
|
| 164 |
|
| 165 |
``` cpp
|
| 166 |
class B { };
|
| 167 |
class D : private B { friend class Derived; };
|
| 168 |
struct Base {
|
|
|
|
| 193 |
Base* bp = &d; // standard conversion:
|
| 194 |
// Derived* to Base*
|
| 195 |
bp->vf1(); // calls Derived::vf1()
|
| 196 |
bp->vf2(); // calls Base::vf2()
|
| 197 |
bp->f(); // calls Base::f() (not virtual)
|
| 198 |
+
B* p = bp->vf4(); // calls Derived::vf4() and converts the
|
| 199 |
// result to B*
|
| 200 |
Derived* dp = &d;
|
| 201 |
+
D* q = dp->vf4(); // calls Derived::vf4() and does not
|
| 202 |
// convert the result to B*
|
| 203 |
+
dp->vf2(); // error: argument mismatch
|
| 204 |
}
|
| 205 |
```
|
| 206 |
|
| 207 |
— *end example*]
|
| 208 |
|
| 209 |
[*Note 3*: The interpretation of the call of a virtual function depends
|
| 210 |
on the type of the object for which it is called (the dynamic type),
|
| 211 |
whereas the interpretation of a call of a non-virtual member function
|
| 212 |
depends only on the type of the pointer or reference denoting that
|
| 213 |
+
object (the static type) [[expr.call]]. — *end note*]
|
| 214 |
|
| 215 |
[*Note 4*: The `virtual` specifier implies membership, so a virtual
|
| 216 |
+
function cannot be a non-member [[dcl.fct.spec]] function. Nor can a
|
| 217 |
virtual function be a static member, since a virtual function call
|
| 218 |
relies on a specific object for determining which function to invoke. A
|
| 219 |
+
virtual function declared in one class can be declared a friend (
|
| 220 |
+
[[class.friend]]) in another class. — *end note*]
|
| 221 |
|
| 222 |
A virtual function declared in a class shall be defined, or declared
|
| 223 |
+
pure [[class.abstract]] in that class, or both; no diagnostic is
|
| 224 |
+
required [[basic.def.odr]].
|
| 225 |
|
| 226 |
+
[*Example 7*:
|
| 227 |
|
| 228 |
Here are some uses of virtual functions with multiple base classes:
|
| 229 |
|
| 230 |
``` cpp
|
| 231 |
struct A {
|
|
|
|
| 248 |
// A* ap = &d; // would be ill-formed: ambiguous
|
| 249 |
B1* b1p = &d;
|
| 250 |
A* ap = b1p;
|
| 251 |
D* dp = &d;
|
| 252 |
ap->f(); // calls D::B1::f
|
| 253 |
+
dp->f(); // error: ambiguous
|
| 254 |
}
|
| 255 |
```
|
| 256 |
|
| 257 |
In class `D` above there are two occurrences of class `A` and hence two
|
| 258 |
occurrences of the virtual member function `A::f`. The final overrider
|
| 259 |
of `B1::A::f` is `B1::f` and the final overrider of `B2::A::f` is
|
| 260 |
`B2::f`.
|
| 261 |
|
| 262 |
— *end example*]
|
| 263 |
|
| 264 |
+
[*Example 8*:
|
| 265 |
|
| 266 |
The following example shows a function that does not have a unique final
|
| 267 |
overrider:
|
| 268 |
|
| 269 |
``` cpp
|
|
|
|
| 277 |
|
| 278 |
struct VB2 : virtual A {
|
| 279 |
void f();
|
| 280 |
};
|
| 281 |
|
| 282 |
+
struct Error : VB1, VB2 { // error
|
| 283 |
};
|
| 284 |
|
| 285 |
struct Okay : VB1, VB2 {
|
| 286 |
void f();
|
| 287 |
};
|
| 288 |
```
|
| 289 |
|
| 290 |
Both `VB1::f` and `VB2::f` override `A::f` but there is no overrider of
|
| 291 |
both of them in class `Error`. This example is therefore ill-formed.
|
| 292 |
+
Class `Okay` is well-formed, however, because `Okay::f` is a final
|
| 293 |
overrider.
|
| 294 |
|
| 295 |
— *end example*]
|
| 296 |
|
| 297 |
+
[*Example 9*:
|
| 298 |
|
| 299 |
The following example uses the well-formed classes from above.
|
| 300 |
|
| 301 |
``` cpp
|
| 302 |
struct VB1a : virtual A { // does not declare f
|
|
|
|
| 311 |
}
|
| 312 |
```
|
| 313 |
|
| 314 |
— *end example*]
|
| 315 |
|
| 316 |
+
Explicit qualification with the scope operator [[expr.prim.id.qual]]
|
| 317 |
suppresses the virtual call mechanism.
|
| 318 |
|
| 319 |
+
[*Example 10*:
|
| 320 |
|
| 321 |
``` cpp
|
| 322 |
class B { public: virtual void f(); };
|
| 323 |
class D : public B { public: void f(); };
|
| 324 |
|
|
|
|
| 328 |
Here, the function call in `D::f` really does call `B::f` and not
|
| 329 |
`D::f`.
|
| 330 |
|
| 331 |
— *end example*]
|
| 332 |
|
| 333 |
+
A function with a deleted definition [[dcl.fct.def]] shall not override
|
| 334 |
+
a function that does not have a deleted definition. Likewise, a function
|
| 335 |
+
that does not have a deleted definition shall not override a function
|
| 336 |
+
with a deleted definition.
|
| 337 |
+
|
| 338 |
+
A `consteval` virtual function shall not override a virtual function
|
| 339 |
+
that is not `consteval`. A `consteval` virtual function shall not be
|
| 340 |
+
overridden by a virtual function that is not `consteval`.
|
| 341 |
|