GCC Newspaper
JULY 29, 2026
Date
/
Architectures
Components
Topics
News & Policy
libstdc++

libstdc++: Extract optional<T&> specialization to bits/optional_ref.h.

Refactors libstdc++ headers to reduce dependencies and resolve cyclic includes.

The optional reference specialization and its related components have been moved to a new header, bits/optional_ref.h. This change aims to break cyclic dependencies, particularly for the header when handling exception_ptr_cast. By separating these definitions, the compiler can avoid issues related to header inclusion order. Additionally, inplace tags have been moved to bits/inplace_tags.h, further minimizing dependencies.

In Details

This commit restructures libstdc++ headers to address cyclic dependencies between <optional>, <exception>, and indirectly <utility>. By extracting std::optional<T&> and its auxiliaries (nullopt_t, _Optional_func, etc.) into bits/optional_ref.h, it resolves an issue preventing exception_ptr_cast from using optional<T&> as a return type. It also moves in_place_t and related tags to bits/inplace_tags.h. Minor ABI-relevant changes include altering std::invoke constraints to is_invocable_v for optional::or_else.

For Context
std::optional<T&>
A specialization of std::optional that can hold a reference to an object. This allows it to represent the presence or absence of a reference, often used in scenarios like returning a reference to a found element or indicating no such element exists.
Cyclic Dependency
A situation in software development where two or more modules or header files directly or indirectly depend on each other, making it difficult to compile or manage the codebase. This often requires refactoring to break the circular references.
exception_ptr_cast
A C++ function typically used with exception handling to cast between different types of exception pointers, allowing for more flexible exception management and propagation.
in_place_t
A type used with placement new or constructors to explicitly indicate that an object should be constructed in-place at a specific memory location, often used with containers or factory functions.
Filed Under: libstdc++headerdependency