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.