GCC Newspaper
JUNE 15, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
libstdc++ Performance Win

libstdc++: Reduce indirection in function_ref where possible.

The `std::function_ref` in libstdc++ avoids double indirection by optimizing construction and assignment.

This commit implements P3961R1, which reduces double indirection in std::function_ref by optimizing its construction and assignment. The implementation expands the set of compatible signatures to include ParamType&& and ParamType, and by-value return types that differ only in cv-qualifiers. This optimization is also applied when function_ref<Ret(Args...) const> is constructed from function_ref<Ret(Args...)>, even if the underlying target can be mutated by the call.

In Details

This commit moves the _M_ptrs members to a new base class __polyfunc::_Ref_base. It defines __is_funcref_assignable for signature compatibility checks during assignment, and adds _ArgsSignature and _TargetQuals member typedefs to function_ref. The __cpp_lib_function_ref feature test macro is updated to 202604. Search snippets should focus on std::function_ref and P3961R1.

For Context

std::function_ref is a non-owning reference to a callable, similar to a function pointer or a lambda. Unlike std::function, it doesn't own the underlying callable, avoiding memory allocation and allowing it to reference functions that are not copyable. This commit optimizes function_ref by avoiding double indirection when possible, improving performance when calling through a function_ref.

Filed Under: libstdc++C++function_refoptimization