https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127576
Bug ID: 127576
Summary: [16 regression] spurious strict aliasing violation w/
typeid & modules
Product: gcc
Version: 16.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: marcel at laverdet dot com
Target Milestone: ---
// dep.cc
export module mod;
export namespace std {
class type_info {
public:
virtual ~type_info();
private:
const char* name_;
};
} // namespace std
export template <class T>
auto type_of() -> const std::type_info& {
return typeid(T);
}
// main.cc
import mod;
struct foo {};
auto f() -> const std::type_info& {
return type_of<foo>();
}
// Makefile
CXXFLAGS = -std=c++20 -O2 -Wall -fmodules
all: dep.o main.o
main.o: main.cc dep.o
%.o: %.cc
$(CXX) $(CXXFLAGS) -c $< -o $@
---
Godbolt: https://godbolt.org/z/Tjxf1Kv4M
$ g++ --version
g++ (Debian 16.2.0-3) 16.2.0
Copyright (C) 2026 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ make
g++ -std=c++20 -O2 -Wall -Werror -fmodules -c dep.cc -o dep.o
g++ -std=c++20 -O2 -Wall -Werror -fmodules -c main.cc -o main.o
In module mod, imported at main.cc:1:
dep.cc: In instantiation of ‘const std::type_info@mod& type_of@mod() [with T =
foo]’:
main.cc:6:21: required from here
6 | return type_of<foo>();
| ~~~~~~~~~~~~^~
dep.cc:15:16: error: dereferencing type-punned pointer will break
strict-aliasing rules [-Werror=strict-aliasing]
15 | return typeid(T);
| ^~~~~~~~~
cc1plus: all warnings being treated as errors
make: *** [Makefile:6: main.o] Error 1
---
Note that I wasn't able to reproduce this on Godbolt 16.2 x86. It reproduces on
trunk though. I'm not sure exactly which versions those correspond to. My copy
of gcc came from apt. This worked on gcc 16.1.