https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119746
Bug ID: 119746
Summary: -Wstrict-aliasing warning on calling pointer to member
function
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: bungeman at chromium dot org
Target Milestone: ---
Godbolt link: https://godbolt.org/z/GxGzzn1Ye
Compiling the following with -Wstrict-aliasing -O2 (or -O3) with GCC 15.0.1
(and back to 8.5)
template <typename T, auto Foo> void DoFoo(T* obj) {
(obj->*Foo)();
}
struct Base {
void foo() const {}
int fRefCnt; /* removing or changing to `char` removes warning */
};
struct Derived : public Base{};
int main() {
Derived derived;
DoFoo<Derived, &Derived::foo>(&derived); /* warns */
//((&derived)->*(&Derived::foo))(); /* does not warn */
//((&derived)->*(&Base::foo))(); /* does not warn */
};
produces
<source>: In instantiation of 'void DoFoo(T*) [with T = Derived; auto Foo =
&Base::foo]':
<source>:14:43: required from here
<source>:2:16: warning: dereferencing type-punned pointer will break
strict-aliasing rules [-Wstrict-aliasing]
(obj->*Foo)();
~~~~~~~~~~~^~
Compiler returned: 0
This is surprising since there aren't any casts here so it isn't clear why any
pointer here would be considered punned.