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

C++/reflection: Correct template lookup in spliced namespaces.

Fixes incorrect template lookup within spliced namespaces in C++ reflection.

A bug in GCC’s C++ reflection implementation caused incorrect template lookups within spliced namespaces. The compiler would perform an unqualified lookup that searched enclosing namespaces instead of a qualified lookup restricted to the target namespace. This commit modifies the lookup mechanism to use lookup_qualified_name first, ensuring that template names are correctly resolved within the intended namespace.

In Details

In gcc/cp/pt.cc, the lookup_template_class function in tsubst was incorrectly using unqualified name lookup (lookup_name) when dealing with TYPENAME_TYPE in the context of reflection's namespace splicing (f<^^N>()). This led to searching enclosing namespaces. The fix changes this to use lookup_qualified_name to find the TEMPLATE_DECL within the specified namespace N, and then passes this found declaration to lookup_template_class. Additionally, error reporting for failed qualified lookups and type constructions is improved.

For Context
C++ Reflection
A C++ feature (partially standardized and evolving) that allows code to inspect and manipulate types and other code constructs at compile time. It involves mechanisms like __reflection or similar proposals.
Spliced Namespace
In C++, a namespace that can be declared multiple times. Subsequent declarations with the same name merge into the original namespace, effectively 'splicing' their contents together.
Template Lookup
The process by which the compiler finds a template definition (e.g., a class template or function template) based on its name and template arguments.
Qualified Name Lookup
A type of name lookup that uses scope resolution operators (like ::) to explicitly specify the namespace or class where a name should be found.
Unqualified Name Lookup
A type of name lookup where the compiler searches for a name starting from the current scope and potentially moving outwards through enclosing scopes, without explicit scope resolution operators.
Filed Under: c++reflectioncompilernamespace