public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/115664] New: -Wnonnull-compare breaks templated methods
@ 2024-06-26 11:30 ossman at cendio dot se
  2024-06-26 14:11 ` [Bug c++/115664] " mpolacek at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: ossman at cendio dot se @ 2024-06-26 11:30 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115664
           Summary: -Wnonnull-compare breaks templated methods
           Product: gcc
           Version: 13.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ossman at cendio dot se
  Target Milestone: ---

A templated method has this protection in it to check that a given argument is
a subclass:

template<class T>
Object::method(void (*callback)(T*))
{
  if (dynamic_cast<T*>(this) == nullptr)
    throw Exception("Bad callback");
  registerCallback(callback);
}

This works fine as long as T is a subclass of Object. However, if T is Object,
then the gcc is clever enough to realise that dynamic_cast<>() does nothing and
the comparison becomes just "if (this == nullptr)", which triggers
-Wnonnull-compare.

The problem is that this is a templated method, so that if statement is
sometimes useful and cannot be removed.

Presently, we have to work around the issue by having a specialised version of
the affected methods, which adds unnecessary duplication.

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

end of thread, other threads:[~2024-07-01  8:56 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-26 11:30 [Bug c++/115664] New: -Wnonnull-compare breaks templated methods ossman at cendio dot se
2024-06-26 14:11 ` [Bug c++/115664] " mpolacek at gcc dot gnu.org
2024-06-26 16:01 ` pinskia at gcc dot gnu.org
2024-06-26 16:05 ` pinskia at gcc dot gnu.org
2024-06-26 16:33 ` pinskia at gcc dot gnu.org
2024-06-26 16:34 ` pinskia at gcc dot gnu.org
2024-06-27 10:07 ` ossman at cendio dot se
2024-06-27 10:55 ` redi at gcc dot gnu.org
2024-06-27 10:58 ` redi at gcc dot gnu.org
2024-07-01  8:56 ` ossman at cendio dot se

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).