tmp/tmp012zkau0/{from.md → to.md}
RENAMED
|
@@ -1,35 +1,32 @@
|
|
| 1 |
-
### Dynamic cast <a id="expr.dynamic.cast">[[expr.dynamic.cast]]</a>
|
| 2 |
|
| 3 |
The result of the expression `dynamic_cast<T>(v)` is the result of
|
| 4 |
converting the expression `v` to type `T`. `T` shall be a pointer or
|
| 5 |
-
reference to a complete class type, or “pointer to
|
| 6 |
-
`dynamic_cast` operator shall not cast away constness
|
| 7 |
-
[[expr.const.cast]]
|
| 8 |
|
| 9 |
If `T` is a pointer type, `v` shall be a prvalue of a pointer to
|
| 10 |
complete class type, and the result is a prvalue of type `T`. If `T` is
|
| 11 |
an lvalue reference type, `v` shall be an lvalue of a complete class
|
| 12 |
type, and the result is an lvalue of the type referred to by `T`. If `T`
|
| 13 |
is an rvalue reference type, `v` shall be a glvalue having a complete
|
| 14 |
class type, and the result is an xvalue of the type referred to by `T`.
|
| 15 |
|
| 16 |
-
If the type of `v` is the same as `T`
|
| 17 |
-
|
| 18 |
-
object type in `v`, the result is `v` (converted if necessary).
|
| 19 |
-
|
| 20 |
-
If the value of `v` is a null pointer value in the pointer case, the
|
| 21 |
-
result is the null pointer value of type `T`.
|
| 22 |
|
| 23 |
If `T` is “pointer to *cv1* `B`” and `v` has type “pointer to *cv2* `D`”
|
| 24 |
such that `B` is a base class of `D`, the result is a pointer to the
|
| 25 |
-
unique `B` subobject of the `D` object pointed to by `v`
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
`D`
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
[*Example 1*:
|
| 33 |
|
| 34 |
``` cpp
|
| 35 |
struct B { };
|
|
@@ -39,37 +36,37 @@ void foo(D* dp) {
|
|
| 39 |
}
|
| 40 |
```
|
| 41 |
|
| 42 |
— *end example*]
|
| 43 |
|
| 44 |
-
Otherwise, `v` shall be a pointer to or a glvalue of a polymorphic
|
| 45 |
-
|
| 46 |
|
| 47 |
-
If `
|
|
|
|
|
|
|
| 48 |
most derived object pointed to by `v`. Otherwise, a runtime check is
|
| 49 |
applied to see if the object pointed or referred to by `v` can be
|
| 50 |
converted to the type pointed or referred to by `T`.
|
| 51 |
|
| 52 |
If `C` is the class type to which `T` points or refers, the runtime
|
| 53 |
check logically executes as follows:
|
| 54 |
|
| 55 |
- If, in the most derived object pointed (referred) to by `v`, `v`
|
| 56 |
-
points (refers) to a
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
result points (refers) to the `C` subobject of the most derived
|
| 64 |
-
object.
|
| 65 |
- Otherwise, the runtime check *fails*.
|
| 66 |
|
| 67 |
The value of a failed cast to pointer type is the null pointer value of
|
| 68 |
the required result type. A failed cast to reference type throws an
|
| 69 |
-
exception
|
| 70 |
-
[[except.handle]]
|
| 71 |
|
| 72 |
[*Example 2*:
|
| 73 |
|
| 74 |
``` cpp
|
| 75 |
class A { virtual void f(); };
|
|
@@ -90,15 +87,16 @@ class E : public D, public B { };
|
|
| 90 |
class F : public E, public D { };
|
| 91 |
void h() {
|
| 92 |
F f;
|
| 93 |
A* ap = &f; // succeeds: finds unique A
|
| 94 |
D* dp = dynamic_cast<D*>(ap); // fails: yields null; f has two D subobjects
|
| 95 |
-
E* ep = (E*)ap; //
|
| 96 |
E* ep1 = dynamic_cast<E*>(ap); // succeeds
|
| 97 |
}
|
| 98 |
```
|
| 99 |
|
| 100 |
— *end example*]
|
| 101 |
|
| 102 |
-
[*Note 1*:
|
| 103 |
-
applied to an object under construction or
|
|
|
|
| 104 |
|
|
|
|
| 1 |
+
#### Dynamic cast <a id="expr.dynamic.cast">[[expr.dynamic.cast]]</a>
|
| 2 |
|
| 3 |
The result of the expression `dynamic_cast<T>(v)` is the result of
|
| 4 |
converting the expression `v` to type `T`. `T` shall be a pointer or
|
| 5 |
+
reference to a complete class type, or “pointer to cv `void`”. The
|
| 6 |
+
`dynamic_cast` operator shall not cast away constness
|
| 7 |
+
[[expr.const.cast]].
|
| 8 |
|
| 9 |
If `T` is a pointer type, `v` shall be a prvalue of a pointer to
|
| 10 |
complete class type, and the result is a prvalue of type `T`. If `T` is
|
| 11 |
an lvalue reference type, `v` shall be an lvalue of a complete class
|
| 12 |
type, and the result is an lvalue of the type referred to by `T`. If `T`
|
| 13 |
is an rvalue reference type, `v` shall be a glvalue having a complete
|
| 14 |
class type, and the result is an xvalue of the type referred to by `T`.
|
| 15 |
|
| 16 |
+
If the type of `v` is the same as `T` (ignoring cv-qualifications), the
|
| 17 |
+
result is `v` (converted if necessary).
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
If `T` is “pointer to *cv1* `B`” and `v` has type “pointer to *cv2* `D`”
|
| 20 |
such that `B` is a base class of `D`, the result is a pointer to the
|
| 21 |
+
unique `B` subobject of the `D` object pointed to by `v`, or a null
|
| 22 |
+
pointer value if `v` is a null pointer value. Similarly, if `T` is
|
| 23 |
+
“reference to *cv1* `B`” and `v` has type *cv2* `D` such that `B` is a
|
| 24 |
+
base class of `D`, the result is the unique `B` subobject of the `D`
|
| 25 |
+
object referred to by `v`.[^14] In both the pointer and reference cases,
|
| 26 |
+
the program is ill-formed if `B` is an inaccessible or ambiguous base
|
| 27 |
+
class of `D`.
|
| 28 |
|
| 29 |
[*Example 1*:
|
| 30 |
|
| 31 |
``` cpp
|
| 32 |
struct B { };
|
|
|
|
| 36 |
}
|
| 37 |
```
|
| 38 |
|
| 39 |
— *end example*]
|
| 40 |
|
| 41 |
+
Otherwise, `v` shall be a pointer to or a glvalue of a polymorphic type
|
| 42 |
+
[[class.virtual]].
|
| 43 |
|
| 44 |
+
If `v` is a null pointer value, the result is a null pointer value.
|
| 45 |
+
|
| 46 |
+
If `T` is “pointer to cv `void`”, then the result is a pointer to the
|
| 47 |
most derived object pointed to by `v`. Otherwise, a runtime check is
|
| 48 |
applied to see if the object pointed or referred to by `v` can be
|
| 49 |
converted to the type pointed or referred to by `T`.
|
| 50 |
|
| 51 |
If `C` is the class type to which `T` points or refers, the runtime
|
| 52 |
check logically executes as follows:
|
| 53 |
|
| 54 |
- If, in the most derived object pointed (referred) to by `v`, `v`
|
| 55 |
+
points (refers) to a public base class subobject of a `C` object, and
|
| 56 |
+
if only one object of type `C` is derived from the subobject pointed
|
| 57 |
+
(referred) to by `v` the result points (refers) to that `C` object.
|
| 58 |
+
- Otherwise, if `v` points (refers) to a public base class subobject of
|
| 59 |
+
the most derived object, and the type of the most derived object has a
|
| 60 |
+
base class, of type `C`, that is unambiguous and public, the result
|
| 61 |
+
points (refers) to the `C` subobject of the most derived object.
|
|
|
|
|
|
|
| 62 |
- Otherwise, the runtime check *fails*.
|
| 63 |
|
| 64 |
The value of a failed cast to pointer type is the null pointer value of
|
| 65 |
the required result type. A failed cast to reference type throws an
|
| 66 |
+
exception [[except.throw]] of a type that would match a handler
|
| 67 |
+
[[except.handle]] of type `std::bad_cast` [[bad.cast]].
|
| 68 |
|
| 69 |
[*Example 2*:
|
| 70 |
|
| 71 |
``` cpp
|
| 72 |
class A { virtual void f(); };
|
|
|
|
| 87 |
class F : public E, public D { };
|
| 88 |
void h() {
|
| 89 |
F f;
|
| 90 |
A* ap = &f; // succeeds: finds unique A
|
| 91 |
D* dp = dynamic_cast<D*>(ap); // fails: yields null; f has two D subobjects
|
| 92 |
+
E* ep = (E*)ap; // error: cast from virtual base
|
| 93 |
E* ep1 = dynamic_cast<E*>(ap); // succeeds
|
| 94 |
}
|
| 95 |
```
|
| 96 |
|
| 97 |
— *end example*]
|
| 98 |
|
| 99 |
+
[*Note 1*: Subclause [[class.cdtor]] describes the behavior of a
|
| 100 |
+
`dynamic_cast` applied to an object under construction or
|
| 101 |
+
destruction. — *end note*]
|
| 102 |
|