https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94616

            Bug ID: 94616
           Summary: Incorrect destruction for partially built objects
           Product: gcc
           Version: 7.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: stephane.zimmerm...@trust-in-soft.com
  Target Milestone: ---

Created attachment 48286
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48286&action=edit
Test file

With the attached file "test.cpp":

extern "C" int printf(const char *, ...);

struct Bar {
    Bar(int n) {
        printf("Bar(%d)\n", n);
        if (n > 0)
            throw 2;
    }

    ~Bar() { printf("~Bar\n"); }
};

struct Foo {
    Bar b1 = 0;
    Bar b2 = 1;
    Foo() { }

    ~Foo() { printf("~Foo()"); }
};

int
main()
{
    try {
        Foo f;
    } catch(int) {
        printf("catch\n");
    }
}

Compiling and running the program yields:
$ g++ test.cpp -fexceptions -std=c++11 && ./a.out
Bar(0)
Bar(1)
terminate called after throwing an instance of 'int'
Aborted (core dumped)

I expected this output:
Bar(0)
Bar(1)
~Bar
catch

As a consequence of n3337[except.ctor]p2:
An object of any storage duration whose initialization or destruction is
terminated by an exception will have destructors executed for all of its fully
constructed subobjects (excluding the variant members of a union-like class),
that is, for subobjects for which the principal constructor ([class.base.init])
has completed execution and the destructor has not yet begun execution.

Reply via email to