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

            Bug ID: 109172
           Summary: g++ calls a private destructor with the keyword throw
                    after the try-block
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: MikeSmith32564 at mail dot com
  Target Milestone: ---

Created attachment 54692
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54692&action=edit
Bug example

In the attached example an instance of class Demo is constructed in-place in a
buffer by following the natural alignment of Demo and then this instance is
passed to throw.
Note that Demo has a private destructor. The expected behavior when building is
that g++ should return a compilation error because a private destructor cannot
be called
outisde of the class Demo or outside of friend declarations. The actual
behavior is that the example builds successfully when

g++ -std=c++11 ./main.cpp -o ./main

is used, producing the following output:

Demo(), id = 1
Demo(const Demo&) called, this->id = 2, other.id = 1
in catch, e.id = 2
~Demo(), id = 2
main ends here

If the destructor is instead declared as deleted the example fails to compile
as expected:

main.cpp: In function 'int main()':
main.cpp:69:10: error: use of deleted function 'Demo::~Demo()'
   throw *d;
          ^

main.cpp:61:2: note: declared here
  ~Demo() = delete;
  ^

Reply via email to