tmp/tmpavdac57j/{from.md → to.md}
RENAMED
|
@@ -1,27 +1,62 @@
|
|
| 1 |
## Constructors and destructors <a id="except.ctor">[[except.ctor]]</a>
|
| 2 |
|
| 3 |
As control passes from the point where an exception is thrown to a
|
| 4 |
-
handler, destructors are invoked
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
|
|
|
| 1 |
## Constructors and destructors <a id="except.ctor">[[except.ctor]]</a>
|
| 2 |
|
| 3 |
As control passes from the point where an exception is thrown to a
|
| 4 |
+
handler, destructors are invoked by a process, specified in this
|
| 5 |
+
section, called *stack unwinding*.
|
| 6 |
+
|
| 7 |
+
The destructor is invoked for each automatic object of class type
|
| 8 |
+
constructed, but not yet destroyed, since the try block was entered. If
|
| 9 |
+
an exception is thrown during the destruction of temporaries or local
|
| 10 |
+
variables for a `return` statement ([[stmt.return]]), the destructor
|
| 11 |
+
for the returned object (if any) is also invoked. The objects are
|
| 12 |
+
destroyed in the reverse order of the completion of their construction.
|
| 13 |
+
|
| 14 |
+
[*Example 1*:
|
| 15 |
+
|
| 16 |
+
``` cpp
|
| 17 |
+
struct A { };
|
| 18 |
+
|
| 19 |
+
struct Y { ~Y() noexcept(false) { throw 0; } };
|
| 20 |
+
|
| 21 |
+
A f() {
|
| 22 |
+
try {
|
| 23 |
+
A a;
|
| 24 |
+
Y y;
|
| 25 |
+
A b;
|
| 26 |
+
return {}; // #1
|
| 27 |
+
} catch (...) {
|
| 28 |
+
}
|
| 29 |
+
return {}; // #2
|
| 30 |
+
}
|
| 31 |
+
```
|
| 32 |
+
|
| 33 |
+
At \#1, the returned object of type `A` is constructed. Then, the local
|
| 34 |
+
variable `b` is destroyed ([[stmt.jump]]). Next, the local variable `y`
|
| 35 |
+
is destroyed, causing stack unwinding, resulting in the destruction of
|
| 36 |
+
the returned object, followed by the destruction of the local variable
|
| 37 |
+
`a`. Finally, the returned object is constructed again at \#2.
|
| 38 |
+
|
| 39 |
+
— *end example*]
|
| 40 |
+
|
| 41 |
+
If the initialization or destruction of an object other than by
|
| 42 |
+
delegating constructor is terminated by an exception, the destructor is
|
| 43 |
+
invoked for each of the object’s direct subobjects and, for a complete
|
| 44 |
+
object, virtual base class subobjects, whose initialization has
|
| 45 |
+
completed ([[dcl.init]]) and whose destructor has not yet begun
|
| 46 |
+
execution, except that in the case of destruction, the variant members
|
| 47 |
+
of a union-like class are not destroyed. The subobjects are destroyed in
|
| 48 |
+
the reverse order of the completion of their construction. Such
|
| 49 |
+
destruction is sequenced before entering a handler of the
|
| 50 |
+
*function-try-block* of the constructor or destructor, if any.
|
| 51 |
+
|
| 52 |
+
If the *compound-statement* of the *function-body* of a delegating
|
| 53 |
+
constructor for an object exits via an exception, the object’s
|
| 54 |
+
destructor is invoked. Such destruction is sequenced before entering a
|
| 55 |
+
handler of the *function-try-block* of a delegating constructor for that
|
| 56 |
+
object, if any.
|
| 57 |
+
|
| 58 |
+
[*Note 1*: If the object was allocated by a *new-expression* (
|
| 59 |
+
[[expr.new]]), the matching deallocation function (
|
| 60 |
+
[[basic.stc.dynamic.deallocation]]), if any, is called to free the
|
| 61 |
+
storage occupied by the object. — *end note*]
|
| 62 |
|