Goto out of a block: do destructors get called?

Alexandre C. picture Alexandre C. · Jul 5, 2010 · Viewed 7.2k times · Source

Consider the following code:

void foo()
{
    {
        CSomeClass bar;

        // Some code here...

        goto label;

        // and here...
    }

label:
    // and here...
}

Will the destructor of bar be called ?

Answer

anon picture anon · Jul 5, 2010

The C++ Standard says:

On exit from a scope (however accomplished), destructors (12.4) are called for all constructed objects with automatic storage duration (3.7.2) (named objects or temporaries) that are declared in that scope, in the reverse order of their declaration.

So the answer is "yes".