On Tue, Jun 16, 2026 at 12:41:09PM +0200, Thomas Schwinge wrote:
> Hi Marek!
>
> On 2023-09-01T20:00:01-0400, Marek Polacek <[email protected]> wrote:
> > When verify_constant complains, it's pretty terse. Consider
> >
> > void test ()
> > {
> > constexpr int i = 42;
> > constexpr const int *p = &i;
> > }
> >
> > where it says "'& i' is not a constant expression". OK, but why?
> >
> > With this patch, we say:
> >
> > b.C:5:28: error: '& i' is not a constant expression
> > 5 | constexpr const int *p = &i;
> > | ^~
> > b.C:5:28: note: pointer to 'i' is not a constant expression
> > b.C:4:17: note: address of non-static constexpr variable 'i' may differ on
> > each invocation of the enclosing function; add 'static' to give it a
> > constant address
> > 4 | constexpr int i = 42;
> > | ^
> > | static
> >
> > which brings g++ on par with clang++.
>
> I noticed that for the following variant code of your
> 'g++.dg/diagnostic/constexpr3.C':
>
> struct A {
> A *ap = this;
> };
>
> constexpr A *
> foo (A *a)
> {
> return a;
> }
>
> void
> test ()
> {
> A a_;
> constexpr A *a = foo (&a_); // { dg-error "not a constant expression" }
> // { dg-message "pointer to .a_. is not a constant expression|may
> differ" "" { xfail *-*-* } .-1 }
> }
>
> ..., GCC still only diagnoses:
>
> constexpr3_.C:18:25: error: '& a_' is not a constant expression
>
> ..., but doesn't provide any further details (XFAILed 'dg-message').
>
> Compare to Clang:
>
> constexpr3_.C:18:16: error: constexpr variable 'a' must be initialized by
> a constant expression
> 18 | constexpr A *a = foo (&a_);
> | ^ ~~~~~~~~~
> constexpr3_.C:18:16: note: pointer to 'a_' is not a constant expression
> constexpr3_.C:17:5: note: declared here
> 17 | A a_;
> | ^
>
> Is (a) my reasoning/expectation correct, and (b) this worth addressing?
Yes and yes. It's a pointer to a local so we could be a bit more helpful
and say that's not a constant expression. I don't see an existing PR (the
original PR was c++/91483).
Marek