Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is guaranteed behaviour. The actual execution is unrolled as follows:</p> <pre><code>0: enter block (scope) 1: Foo::Foo() 2. evaluation of bar(); as expression in return statement 3. save result of the expression as value returned from function 4. finalize return statement to leave function to its caller (request exit from current scope) 5: exit block (scope) with call to Foo::~Foo() </code></pre> <p>Here are some references from the standard:</p> <ul> <li>What program execution guarantees, generally</li> </ul> <blockquote> <p>1.9 Program execution</p> <p>10 An instance of each object with automatic storage duration (3.7.2) is associated with each entry into its block.</p> </blockquote> <ul> <li>The <code>foo</code> is of automatic storage duration and:</li> </ul> <blockquote> <p>3.7.2 Automatic storage duration</p> <p>1 Local objects explicitly declared auto or register or not explicitly declared static or extern have automatic storage duration. The storage for these objects lasts until the block in which they are created exits.</p> </blockquote> <ul> <li>What is actual effect of return statement</li> </ul> <blockquote> <p>6.6.3 The return statement</p> <p>2 (...) the value of the expression is returned to the caller of the function</p> </blockquote> <p>and</p> <blockquote> <p>6.6 Jump statements <em>(return belongs to jump statements)</em></p> <p>2 On exit from a scope (however accomplished), destructors (12.4) are called for all constructed objects with automatic storage duration (3.7.2)</p> </blockquote> <ul> <li>What guarantees that the effect occurs</li> </ul> <blockquote> <p>6.7 Declaration statement</p> <p>2 Variables with automatic storage duration declared in the block are destroyed on exit from the block</p> </blockquote> <p>and</p> <blockquote> <p>12.4 Destructors </p> <p>10 Destructors are invoked implicitly (1) for a constructed object with static storage duration (3.7.1) at program termination (3.6.3), (2) for a constructed object with automatic storage duration (3.7.2) when the block in which the object is created exits (6.7)</p> </blockquote> <p>It is not easy to grasp single idea form details scattered around all the C++ standard. Hopefully, quick overview will help you to make such analysis yourself too.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload