[Bug c++/95977] No deallocation of temporary in return-statement during constant evaluation

pkeir at outlook dot com gcc-bugzilla@gcc.gnu.org
Wed Jul 29 00:11:33 GMT 2020


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

Paul Keir <pkeir at outlook dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pkeir at outlook dot com

--- Comment #2 from Paul Keir <pkeir at outlook dot com> ---
I've also encountered this bug. I changed your code to:

struct X {
    int* x{new int{42}};
    constexpr ~X() { delete x; }
};

int main(int argc, char *argv[])
{
  static_assert(42 == *X{}.x);
  return 0;
}

...and now an extra note appears at compilation:

note: ‘<anonymous>’ was not declared ‘constexpr’

15 |   static_assert(42 == *X{}.x);
   |                          ^

This made me think of the implicitly-defined default constructor for X, which
should be `constexpr`. But perhaps attribution of this `constexpr` qualifier is
failing due to the memory not having been freed at the point where the equality
is evaluated? This is certainly unintuitive, but I do note that the error
disappears when `static_assert(doit())` is used instead, with the following
definition: 

constexpr bool doit()
{
  int i = *X{}.x;
  return i==42;
}

...but while that perhaps make sense, I encountered two other changes to the
version above which surprisingly also remove the error:

1) Adding an explicitly defaulted constructor for X;
2) Using `X()` rather than `X{}` in the static assert.


More information about the Gcc-bugs mailing list