https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96510
Bug ID: 96510
Summary: Unexpected constexpr deallocation error after implicit
conversion
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: pkeir at outlook dot com
Target Milestone: ---
Created attachment 49012
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49012&action=edit
A C++ file containing the code described.
In the C++20 code below (also attached), the Foo class allocates and
deallocates to an int pointer member in the constructor and destructor
respectively. Comparing a Foo object to an integer (via `operator==`) makes use
of another member function `compare` which expects a `Foo&`; and so a `Foo`
object is implicitly created. Somehow, within a `constexpr` context (e.g. the
`static_assert` below), the `constexpr` memory is not being freed, and an error
message is issued (error: ‘do_stuff()’ is not a constant expression because
allocated storage has not been deallocated).
struct Foo {
constexpr Foo(int i) { p = new int(i); }
constexpr ~Foo() { delete p; }
constexpr bool compare(const Foo&) const { return true; }
constexpr bool operator==(int y) const { return compare(y); }
int *p;
};
constexpr bool do_stuff()
{
Foo f(0);
bool b = f == 42;
return b;
}
int main(int argc, char *argv[])
{
static_assert(do_stuff());
return do_stuff() ? 0 : -1;
}