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

C++ contracts regeneration from templates is now always performed.

C++ contracts are now always regenerated from the current code pattern in templates, resolving issues with explicit instantiations.

This commit ensures that C++ contracts are always regenerated from the current code pattern within regenerate_decl_from_template() when either the declaration or the pattern has contracts. This addresses an issue where uninstantiated contracts associated with a function template instantiation could incorrectly refer to parameter declarations from an earlier definition if an explicit instantiation was encountered before a later redeclaration. By always regenerating from the current pattern, the compiler ensures that contracts accurately reflect the latest definition, preventing potential errors related to stale parameter references.

In Details

The regenerate_decl_from_template function in gcc/cp/pt.cc is responsible for creating a specialized declaration from a template. When dealing with C++ contracts, it previously associated uninstantiated contracts with template instantiations. If an explicit instantiation of a template occurred before a subsequent redeclaration that redefined the function or its contracts, the contracts might still reference parameter declarations (PARM_DECL) from the older version. This change modifies regenerate_decl_from_template to unconditionally regenerate contracts from the code_pattern if con…

For Context
C++ contracts
A C++ language feature (tentatively C++26) that allows developers to specify contract conditions (preconditions, postconditions, assertions) directly in the code. These contracts can be used for static analysis, runtime checking, and documentation.
template instantiation
The process by which a function template or class template is converted into a concrete function or class by substituting template arguments for template parameters.
explicit instantiation
A C++ construct that forces the instantiation of a template for a specific set of arguments, even if it's not directly used in the code.
PARM_DECL
A GCC internal representation for a function parameter declaration.
Filed Under: c++contractstemplates