Implement C++26 std::is_within_lifetime extension.
C++26 `std::is_within_lifetime` is now implemented in libstdc++ using compiler built-ins.
GCC’s libstdc++ now implements the C++26 proposal P3450R1, which extends std::is_within_lifetime. This implementation uses __builtin_constant_p to determine if a static_cast mandated by the standard can be evaluated as a constant expression. New test cases verify this functionality.
In Details
This commit implements the C++26 P3450R1 paper in libstdc++'s <type_traits>. The std::is_within_lifetime type trait now accepts an optional _Up template parameter, defaulting to void. It leverages __builtin_constant_p to check if the static_cast<const volatile _Up*>(__p) expression, which is part of the standard's UB diagnosis for pointer conversions, is a compile-time constant. This follows up on an earlier change that enabled constexpr evaluation of such downcasts.
- std::is_within_lifetime
- A C++ type trait that checks if a given pointer is within the lifetime of a specified object. The C++26 extension adds flexibility for specific pointer conversions.
- P3450R1
- A C++ standardization proposal document (Revision 1) titled 'Extend std::is_within_lifetime'.
- __builtin_constant_p
- A GCC built-in function that returns non-zero if its argument is a compile-time constant, and zero otherwise. Used to enable compile-time optimizations and checks.
- constexpr evaluation
- The process by which the compiler evaluates expressions or function calls at compile time, rather than at runtime. This can enable optimizations and is required for certain language features.
- UB
- Undefined Behavior. In C++, this means the program's behavior is unpredictable and can lead to crashes, incorrect results, or security vulnerabilities. Compilers often try to detect and diagnose UB.
- static_cast
- A C++ casting operator used for explicit conversions between related types, such as converting a base class pointer to a derived class pointer (downcasting).