C++26: std::is_within_lifetime | Sandor Dargo’s Blog Sandor Dargo’s Blog On C++, software development and books HOME TAGS ARCHIVES BOOKS SPEAKING DAILY C++ HI… Blog 2026 02 18 C++26: std::is_within_lifetime Post Cancel C++26: std::is_within_lifetime Sandor Dargo Feb 18 2026-02-18T00:00:00+01:00 4 min When I was looking for the next topic for my posts, my eyes stopped on std::is_within_lifetime . Dealing with lifetime issues is a quite common source of bugs, after all. Then I clicked on the link and I read Checking if a union alternative is active . I scratched my head. Is the link correct? It is — and it totally makes sense. Let’s get into the details and first check what P2641R4 is about. What does std::is_within_lifetime do? C++26 adds bool std::is_within_lifetime(const T* p) to the header. This function checks whether p points to an object that is currently within its lifetime during constant evaluation. The most common use case is checking which member of a union is currently active. Here’s a simple example: 1 2 3 4 5 6 7 8 9 10 11 12 union Storage { int i ; double d ; }; constexpr bool check_active_member () { Storage s ; s . i = 42 ; // At this point, ‘i’ is the active member return std :: is_within_lifetime ( & s . i ); // returns true } In this example, after assigning to s.i , that member becomes active. The function std::is_within_lifetime(&s.i) returns true , confirming that i is within its lifetime. If we checked std::is_within_lifetime(&s.d) at this point, it would return false since d is not the active member. Properties and the name The function has some interesting design choices that are worth discussing. It’s consteval only std::is_within_lifetime is consteval , meaning it can only be used during compile-time. You cannot call it at runtime. This might seem limiting, but it’s actually by design. The purpose of this function is to solve problems that exist specifically in the constant evaluation world. At runtime, you have other mechanisms available like tracking state with additional variables. The compiler doesn’t maintain the same level of lifetime tracking information at runtime that it does during constant evaluation. Why a pointer instead of a reference? The function takes a pointer rather than a reference, which might seem unusual for a query operation. The reasoning is straightforward: passing by reference can introduce complications with temporary objects and lifetime extension rules. A pointer makes the intent explicit — you’re asking about a specific memory location, not about a value or a reference that might be bound to various things. It’s a cleaner semantic fit for what the function actually does. Why not “ is_union_member_active ”? You might wonder why the feature has such a general name when the primary use case is specifically about unions. The answer is that the committee chose to solve the problem at a more fundamental level. Instead of adding a union-specific check, they provided a general mech
Source: Hacker News | Original Link