public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100861] New: False positive -Wmismatched-new-delete with destroying operator delete
@ 2021-06-01 19:43 josephcsible at gmail dot com
  2021-06-01 21:36 ` [Bug middle-end/100861] " msebor at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: josephcsible at gmail dot com @ 2021-06-01 19:43 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100861
           Summary: False positive -Wmismatched-new-delete with destroying
                    operator delete
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: josephcsible at gmail dot com
  Target Milestone: ---

Currently, code like "Base *b = new Derived; delete b;" gives a
-Wmismatched-new-delete warning, unless Base has a virtual destructor. One of
the use cases for C++20's destroying operator delete is to make this safe
without needing a virtual destructor, so this warning should be suppressed if
Base has a destroying operator delete. Here's a modified version of the code at
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0722r1.html that will
unnecessarily cause this warning (compile with -std=c++20 -Wall):

#include <new>

enum class WidgetKind {
  Grommet,
  Wingnut
};

struct Widget {
  const WidgetKind Kind : 4;
  unsigned OtherThings : 28;

  Widget(WidgetKind k) : Kind(k) {}
  void operator delete(Widget *, std::destroying_delete_t);
};

struct Grommet : Widget {
  Grommet() : Widget(WidgetKind::Grommet) {}
};

struct Wingnut : Widget {
  Wingnut() : Widget(WidgetKind::Wingnut) {}
};

void Widget::operator delete(Widget *widget, std::destroying_delete_t) {
  switch (widget->Kind) {
  case WidgetKind::Grommet:
    static_cast<Grommet*>(widget)->~Grommet();
    ::operator delete(widget, sizeof(Grommet));
    return;
  case WidgetKind::Wingnut:
    static_cast<Wingnut*>(widget)->~Wingnut();
    ::operator delete(widget, sizeof(Wingnut));
    return;
  }
}

int main() {
  Widget *widget = new Grommet;
  delete widget;
}

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

end of thread, other threads:[~2023-12-22 18:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-01 19:43 [Bug c++/100861] New: False positive -Wmismatched-new-delete with destroying operator delete josephcsible at gmail dot com
2021-06-01 21:36 ` [Bug middle-end/100861] " msebor at gcc dot gnu.org
2021-06-01 21:49 ` josephcsible at gmail dot com
2021-06-01 22:08 ` msebor at gcc dot gnu.org
2023-12-22 18:31 ` 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).