Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The behavior is undefined because the destructor is invoked twice for the same object:</p> <ul> <li>Once when you invoke it explicitly</li> <li>Once when the scope ends and the automatic variable is destroyed</li> </ul> <p>Invoking the destructor on an object whose lifetime has ended results in undefined behavior per C++03 §12.4/6:</p> <blockquote> <p>the behavior is undefined if the destructor is invoked for an object whose lifetime has ended</p> </blockquote> <p>An object's lifetime ends when its destructor is called per §3.8/1:</p> <blockquote> <p>The lifetime of an object of type <code>T</code> ends when:</p> <p>— if <code>T</code> is a class type with a non-trivial destructor (12.4), the destructor call starts, or</p> <p>— the storage which the object occupies is reused or released.</p> </blockquote> <p>Note that this means if your class has a trivial destructor, the behavior is well-defined because the lifetime of an object of such a type does not end until its storage is released, which for automatic variables does not happen until the end of the function. Of course, I don't know why you would explicitly invoke the destructor if it is trivial. </p> <p>What is a trivial destructor? §12.4/3 says:</p> <blockquote> <p>A destructor is trivial if it is an implicitly-declared destructor and if:</p> <p>— all of the direct base classes of its class have trivial destructors and</p> <p>— for all of the non-static data members of its class that are of class type (or array thereof), each such class has a trivial destructor.</p> </blockquote> <p>As others have mentioned, one possible result of undefined behavior is your program appearing to continue running correctly; another possible result is your program crashing. Anything can happen and there are no guarantees whatsoever.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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