[PATCH] c++, libstdc++: Implement C++26 P2641R4 - Checking if a union alternative is active

Jakub Jelinek jakub@redhat.com
Wed Aug 26 16:57:23 GMT 2026


On Wed, Aug 26, 2026 at 11:50:37AM +0200, Tomasz Kaminski wrote:
> >
> Any reason not to check &c.c (array) itself>

Added in my copy, incremental
--- a/gcc/testsuite/g++.dg/cpp26/within-lifetime1.C	2026-08-25 16:58:17.000000000 +0200
+++ b/gcc/testsuite/g++.dg/cpp26/within-lifetime1.C	2026-08-26 18:42:14.645233986 +0200
@@ -335,6 +335,8 @@ foo ()
     return 141;
   if (!std::is_within_lifetime (t + 1))
     return 142;
+  if (!std::is_within_lifetime (&c.c))
+    return 143;
   return 0;
 }
 
added at the end as I don't want to renumber everything.
Maybe I should have used
    return __LINE__;
in all cases.  Changed that now too.

> > +  {
> > +    int c = 42;
> > +    if (x == 1)
> > +      __builtin_is_within_lifetime (a);        // { dg-error
> > "'__builtin_within_lifetime' on 'c' outside its lifetime is not a constant
> > expression" }
> >
> That's message should very confusing,  '__builtin_within_lifetime' on 'c'
> outside its lifetime is not a constant expression", while
> the purpose of this function is to check if they are within the lifetime.
> Maybe we should say outside it's storage duration?

But it is really about lifetime of the complete object.
Perhaps I should add the "complete object " to the message to make it clear?

> > +
> > +struct A { int a = 42; bool b = std::is_within_lifetime (&a); };       //
> > { dg-error "" "" { xfail *-*-* } }
> >
> What are the errors here and for next example? The member lifetime seems
> clear at corresponding points.

There are no errors, that is the problem.
clang++ reports on https://godbolt.org/z/dKM9q9163
<source>:4:13: error: constexpr variable 'n' must be initialized by a constant expression
<source>:3:33: note: 'std::is_within_lifetime' cannot be called with a pointer to an object whose lifetime has not yet begun
...
<source>:7:13: error: constexpr variable 'o' must be initialized by a constant expression
<source>:6:40: note: 'std::is_within_lifetime' cannot be called with a pointer to an object whose lifetime has not yet begun
...
<source>:9:70: error: call to consteval function 'C::C' is not a constant expression
<source>:9:29: note: '__builtin_is_within_lifetime' cannot be called with a pointer to an object whose lifetime has not yet begun
    9 | struct C { consteval C () { __builtin_is_within_lifetime (this); } } p; // { dg-error "" "" { xfail *-*-* } }
      |                                                                      ^

In all the 3 cases the complete object lifetime has not started yet (because
it is still being constructed).

> > +constexpr A n;
> >
> I.e. a is in lifetime here.

But n is not yet in lifetime.

> > +
> > +struct B { consteval B () : a (42), b (std::is_within_lifetime (&a)) {}
> > int a; bool b; };
> >
> And not in lifetime here.

If the complete object would be in lifetime (see the testcase in the
description rather than patch), I think a would be in lifetime here, but
not b itself nor some nsdm initialized in a later mem-initializer.

> > +  static_assert (&corge (0));
> > +  static_assert (&corge (A {}));

I have actually meant __builtin_is_within_lifetime calls in the
static_asserts too, added in my copy.

	Jakub



More information about the Libstdc++ mailing list