From Jason Turner

[except.ctor]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpavdac57j/{from.md → to.md} +58 -23
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 for all automatic objects constructed
5
- since the try block was entered. The automatic objects are destroyed in
6
- the reverse order of the completion of their construction.
7
-
8
- An object of any storage duration whose initialization or destruction is
9
- terminated by an exception will have destructors executed for all of its
10
- fully constructed subobjects (excluding the variant members of a
11
- union-like class), that is, for subobjects for which the principal
12
- constructor ([[class.base.init]]) has completed execution and the
13
- destructor has not yet begun execution. Similarly, if the non-delegating
14
- constructor for an object has completed execution and a delegating
15
- constructor for that object exits with an exception, the object’s
16
- destructor will be invoked. If the object was allocated in a
17
- *new-expression*, the matching deallocation function (
18
- [[basic.stc.dynamic.deallocation]], [[expr.new]], [[class.free]]), if
19
- any, is called to free the storage occupied by the object.
20
-
21
- The process of calling destructors for automatic objects constructed on
22
- the path from a try block to the point where an exception is thrown is
23
- called “*stack unwinding*.” If a destructor called during stack
24
- unwinding exits with an exception, `std::terminate` is called (
25
- [[except.terminate]]). So destructors should generally catch exceptions
26
- and not let them propagate out of the destructor.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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