[Bug c++/101442] [9/10/11/12 Regression] Destructor not called for a temporary object, if it's bound to a ref member of an object subject to NRVO

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Jul 14 09:45:18 GMT 2021


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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Reduced:

bool destroyed = false;

struct A
{
    A() {}
    A(const A &) = delete;
    A &operator=(const A &) = delete;
    ~A() {destroyed = true;}
};

struct B
{
    const A &a;
    struct string {
     string(const char*) { }
    ~string() { }
    } s;
};

B foo()
{
    B ret{ A{}, "" };
    return ret;
}

int main()
{
  {
    B b = foo();
  }
  if (!destroyed)
    __builtin_abort();
}


More information about the Gcc-bugs mailing list