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

Implement C++26 P2641R4 - Checking if a union alternative is active

Implements C++26 P2641R4 in libstdc++ for checking union active members at compile time using `__builtin_is_within_lifetime`.

This patch implements the C++26 feature P2641R4, allowing compile-time checks for which member of a union is currently active. It introduces a builtin function that returns true if a given pointer points to an active union member, with specific handling for constant expressions and lifetime rules. This aligns GCC’s libstdc++ with Clang’s implementation of the paper, enhancing type safety and enabling more robust compile-time evaluations.

In Details

Implements C++26 P2641R4 (std::is_within_lifetime) using GCC's __builtin_is_within_lifetime. The builtin checks if a pointer __p points into the lifetime of an object. For unions, this specifically verifies if __p refers to the currently active member. This patch defers implementation of P3450R1 and correct std::allocator constant evaluation, which depend on further changes (e.g., CONSTRUCTOR bit). The test case within-lifetime3.C is included but partially guarded.

For Context
libstdc++
The standard C++ library implementation used by GCC.
union
A user-defined data type that can store different data types in the same memory location, but only one at a time.
P2641R4
A C++ standard proposal (Revision 4) that introduces a mechanism to check which alternative in a union is active at compile time.
compile-time evaluation
The process of evaluating expressions or functions during the compilation phase, rather than at runtime. This is often referred to as 'constexpr' or 'consteval' evaluation in C++.
__builtin_is_within_lifetime
A GCC-specific intrinsic function that checks if a given pointer points within the allocated lifetime of an object.
Filed Under: gcclibstdc++C++26compile timetype safety