public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/96510] New: Unexpected constexpr deallocation error after implicit conversion
@ 2020-08-06 23:29 pkeir at outlook dot com
  2020-08-29  1:07 ` [Bug c++/96510] " johelegp at gmail dot com
  2021-12-08  8:29 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: pkeir at outlook dot com @ 2020-08-06 23:29 UTC (permalink / raw)
  To: gcc-bugs

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;
}

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-12-08  8:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-06 23:29 [Bug c++/96510] New: Unexpected constexpr deallocation error after implicit conversion pkeir at outlook dot com
2020-08-29  1:07 ` [Bug c++/96510] " johelegp at gmail dot com
2021-12-08  8:29 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).