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

C++/reflection: Fix extract-ref to correctly handle variable lifetime and constant expressions

C++ reflection's `extract-ref` is fixed to correctly evaluate variable lifetime and constant expression constraints, resolving crashes and improving accuracy.

This commit corrects the extract-ref functionality within C++’s reflection capabilities to align with the standard’s requirements regarding variable lifetime and constant expressions. Previously, incorrect checks led to crashes when evaluating variables. The fix ensures that extract-ref properly distinguishes between variables usable in constant expressions and those whose lifetimes began within the current evaluation context. It also addresses how parameter declarations are handled and ensures that results are correctly wrapped to maintain type fidelity, resolving PR127232.

In Details

This patch for GCC's C++ front-end (cp/constexpr.cc, cp-tree.h, cp/reflect.cc) rectifies issues in reflect::extract_ref concerning PR127232. It refines the checks against [meta.reflection.extract]/5.3 by introducing constexpr_global_ctx::in_eval_lifetime_p and cxx_constexpr_in_eval_lifetime_p to accurately determine if a variable's lifetime began within the current constant evaluation. This replaces the imprecise is_constant_expression check, preventing crashes when build_address receives types instead of variables. The patch also corrects handling of DECL_INITIAL for `PARM_…

For Context
C++ Reflection
A C++ language feature (currently a proposal) that allows programs to inspect and manipulate their own structure at runtime or compile time, such as examining types, variables, and functions.
extract-ref
A specific operation or function within the C++ reflection mechanism designed to extract a reference to a variable or entity.
Constant expression
An expression in C++ that can be evaluated at compile time. Variables usable in constant expressions must meet certain criteria, such as being const and having their initializer evaluated at compile time.
Lifetime
The duration during which an object or variable is valid and can be accessed in C++. For constant expressions, this includes whether the object's lifetime began within the current evaluation context.
PARM_DECL
A GCC internal representation (tree node) for a function parameter declaration.
NOP_EXPR
An intermediate representation (IR) node in GCC that represents a no-operation. It is often used to wrap expressions or values without changing their meaning, sometimes to satisfy type or conversion requirements.
Filed Under: c++reflectionconstexprcompiler bugs