GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
c++

c++: Fix modules and typeid interaction for template instantiation.

c++: Ensures `typeid` works correctly with modules by reusing existing type information.

This commit addresses an issue where instantiating a template from a translation unit (TU) that included <typeinfo> in an importing TU that did not, would fail. Instead of complex lookup mechanisms in the other TU, the change reuses the type information already associated with the TYPEID_EXPR. This ensures consistent behavior for typeid when using C++ modules.

In Details

In C++ with modules, type information can be inconsistent across translation units. Specifically, if a TU importing another TU includes <typeinfo> while the imported TU does not, instantiating templates that use typeid can fail. This patch modifies tsubst_expr, build_typeid, and get_typeid in pt.cc and rtti.cc to pass the type directly to build_typeid and get_typeid, thus reusing the TYPEID_EXPR's existing type information instead of performing cross-TU lookups. This resolves PR124888.

For Context
modules
A C++ language feature (since C++20) that allows code to be organized into named units, improving build times and encapsulation by providing explicit interfaces between code units.
typeid
A C++ operator that returns a std::type_info object representing the type of an expression. This object can be used for runtime type identification.
template instantiation
The process by which a C++ template (e.g., a class or function template) is converted into a concrete class or function with specific types or values provided for its template parameters.
translation unit (TU)
A single source file after preprocessing. In C++, this is typically a .cpp file and its included header files, compiled into an object file.
TYPEID_EXPR
GCC's internal representation (likely a GIMPLE or tree node) for a typeid expression encountered during parsing or intermediate representation processing.
PR124888
A bug report number specific to the GCC bug tracking system, indicating a known issue that this commit aims to fix.
Filed Under: c++modulesrtti