https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103843
Bug ID: 103843 Summary: Direct call to Desctructor is optimized out Product: gcc Version: 10.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: georgii.sha...@be-tse.de Target Milestone: --- cat ./d.cpp #include <iostream> class S { public: S() = default; ~S() { i=10; } void foo() { this->~S(); } int getI() const { return i; } private: int i{0}; }; int main() { S s; do { std::cout << "Before foo: " << s.getI(); s.foo(); std::cout << "; After: " << s.getI() << std::endl; } while (false); return 0; } g++ -O0 ./d.cpp $./a.out Before foo: 0; After: 10 g++ -O3 ./d.cpp georgii@ltgscosvm:~/prj/test$./a.out Before foo: 0; After: 0 gcc --version gcc (GCC) 10.2.1 20210130 (Red Hat 10.2.1-11) Copyright (C) 2020 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. hostnamectl Static hostname: ltgscosvm.be-tse01.de Icon name: computer-vm Chassis: vm Machine ID: fb944a0ffb46449f9b639e589d00b598 Boot ID: 433e59a4db5c419a9081cc6968e4e590 Virtualization: microsoft Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.45.1.el7.x86_64 Architecture: x86-64 uname -a Linux ltgscosvm.be-tse01.de 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux