[Bug c++/103843] Direct call to Desctructor is optimized out

georgii.shagov@be-tse.de gcc-bugzilla@gcc.gnu.org
Mon Dec 27 09:33:36 GMT 2021


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

--- Comment #1 from Georgii.Shagov <georgii.shagov@be-tse.de> ---
>From experiments, I would guess that in case of -O3 the call to destructor was
substituted by initializer:

This works fine:

cat ./d.cpp
#include <iostream>

class S {
public:
   S() : i{1} {}
   ~S() { i=0; }
   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++ ./d.cpp 
$./a.out 
Before foo: 1; After: 0
$g++ -O3 ./d.cpp 
$./a.out 
Before foo: 1; After: 0
g++ --version
g++ (GCC) 10.2.1 20210130 (Red Hat 10.2.1-11)


More information about the Gcc-bugs mailing list