From Jason Turner

[expr.dynamic.cast]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpazlfevpp/{from.md → to.md} +22 -18
tmp/tmpazlfevpp/{from.md → to.md} RENAMED
@@ -1,20 +1,19 @@
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 an expression having a
14
- complete class type, and the result is an xvalue of the type referred to
15
- by `T`.
16
 
17
  If the type of `v` is the same as `T`, or it is the same as `T` except
18
  that the class object type in `T` is more cv-qualified than the class
19
  object type in `v`, the result is `v` (converted if necessary).
20
 
@@ -24,33 +23,35 @@ result is the null pointer value of type `T`.
24
  If `T` is “pointer to *cv1* `B`” and `v` has type “pointer to *cv2* `D`”
25
  such that `B` is a base class of `D`, the result is a pointer to the
26
  unique `B` subobject of the `D` object pointed to by `v`. Similarly, if
27
  `T` is “reference to *cv1* `B`” and `v` has type *cv2* `D` such that `B`
28
  is a base class of `D`, the result is the unique `B` subobject of the
29
- `D` object referred to by `v`. [^8] The result is an lvalue if `T` is an
30
- lvalue reference, or an xvalue if `T` is an rvalue reference. In both
31
- the pointer and reference cases, the program is ill-formed if *cv2* has
32
- greater cv-qualification than *cv1* or if `B` is an inaccessible or
33
- ambiguous base class of `D`.
34
 
35
  ``` cpp
36
  struct B { };
37
  struct D : B { };
38
  void foo(D* dp) {
39
  B* bp = dynamic_cast<B*>(dp); // equivalent to B* bp = dp;
40
  }
41
  ```
42
 
 
 
43
  Otherwise, `v` shall be a pointer to or a glvalue of a polymorphic
44
  type ([[class.virtual]]).
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 run-time 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 run-time
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,
56
  and if only one object of type `C` is derived from the subobject
@@ -59,17 +60,19 @@ check logically executes as follows:
59
  - Otherwise, if `v` points (refers) to a `public` base class subobject
60
  of the most derived object, and the type of the most derived object
61
  has a base class, of type `C`, that is unambiguous and `public`, the
62
  result points (refers) to the `C` subobject of the most derived
63
  object.
64
- - Otherwise, the run-time check *fails*.
65
 
66
  The value of a failed cast to pointer type is the null pointer value of
67
  the required result type. A failed cast to reference type throws an
68
  exception ([[except.throw]]) of a type that would match a handler (
69
  [[except.handle]]) of type `std::bad_cast` ([[bad.cast]]).
70
 
 
 
71
  ``` cpp
72
  class A { virtual void f(); };
73
  class B { virtual void g(); };
74
  class D : public virtual A, private B { };
75
  void g() {
@@ -78,23 +81,24 @@ void g() {
78
  A* ap = &d; // public derivation, no cast needed
79
  D& dr = dynamic_cast<D&>(*bp); // fails
80
  ap = dynamic_cast<A*>(bp); // fails
81
  bp = dynamic_cast<B*>(ap); // fails
82
  ap = dynamic_cast<A*>(&d); // succeeds
83
- bp = dynamic_cast<B*>(&d); // ill-formed (not a run-time check)
84
  }
85
 
86
  class E : public D, public B { };
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 0
92
- // f has two D subobjects
93
  E* ep = (E*)ap; // ill-formed: cast from virtual base
94
  E* ep1 = dynamic_cast<E*>(ap); // succeeds
95
  }
96
  ```
97
 
98
- [[class.cdtor]] describes the behavior of a `dynamic_cast` applied to an
99
- object under construction or destruction.
 
 
100
 
 
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`, or it is the same as `T` except
17
  that the class object type in `T` is more cv-qualified than the class
18
  object type in `v`, the result is `v` (converted if necessary).
19
 
 
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`. Similarly, if
26
  `T` is “reference to *cv1* `B`” and `v` has type *cv2* `D` such that `B`
27
  is a base class of `D`, the result is the unique `B` subobject of the
28
+ `D` object referred to by `v`.[^8] In both the pointer and reference
29
+ cases, the program is ill-formed if *cv2* has greater cv-qualification
30
+ than *cv1* or if `B` is an inaccessible or ambiguous base class of `D`.
31
+
32
+ [*Example 1*:
33
 
34
  ``` cpp
35
  struct B { };
36
  struct D : B { };
37
  void foo(D* dp) {
38
  B* bp = dynamic_cast<B*>(dp); // equivalent to B* bp = dp;
39
  }
40
  ```
41
 
42
+ — *end example*]
43
+
44
  Otherwise, `v` shall be a pointer to or a glvalue of a polymorphic
45
  type ([[class.virtual]]).
46
 
47
+ If `T` is “pointer to *cv* `void`”, then the result is a pointer to the
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 `public` base class subobject of a `C` object,
57
  and if only one object of type `C` is derived from the subobject
 
60
  - Otherwise, if `v` points (refers) to a `public` base class subobject
61
  of the most derived object, and the type of the most derived object
62
  has a base class, of type `C`, that is unambiguous and `public`, the
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 ([[except.throw]]) of a type that would match a handler (
70
  [[except.handle]]) of type `std::bad_cast` ([[bad.cast]]).
71
 
72
+ [*Example 2*:
73
+
74
  ``` cpp
75
  class A { virtual void f(); };
76
  class B { virtual void g(); };
77
  class D : public virtual A, private B { };
78
  void g() {
 
81
  A* ap = &d; // public derivation, no cast needed
82
  D& dr = dynamic_cast<D&>(*bp); // fails
83
  ap = dynamic_cast<A*>(bp); // fails
84
  bp = dynamic_cast<B*>(ap); // fails
85
  ap = dynamic_cast<A*>(&d); // succeeds
86
+ bp = dynamic_cast<B*>(&d); // ill-formed (not a runtime check)
87
  }
88
 
89
  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; // ill-formed: cast from virtual base
96
  E* ep1 = dynamic_cast<E*>(ap); // succeeds
97
  }
98
  ```
99
 
100
+ *end example*]
101
+
102
+ [*Note 1*: [[class.cdtor]] describes the behavior of a `dynamic_cast`
103
+ applied to an object under construction or destruction. — *end note*]
104