Bug 85428 - constexpr pointer equality comparison not considered constant expression
Summary: constexpr pointer equality comparison not considered constant expression
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.3.0
: P3 normal
Target Milestone: 12.0
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
: 91693 (view as bug list)
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2018-04-17 08:53 UTC by stinkingmadgod
Modified: 2022-01-06 15:56 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-04-17 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description stinkingmadgod 2018-04-17 08:53:11 UTC
The following should compile

template<typename T>
struct S
{
    inline static char c;
};

void foo()
{
    constexpr auto t1 = &S<int>::c;
    constexpr auto t2 = &S<char>::c;
    static_assert(t1 != t2);
}

But won't, with error: non-constant condition for static assertion.

[expr.eq] says the pointers is specified to be unequal, and [expr.const] says this is a constant expression.

However this compiles

inline char c1, c2;

void bar()
{
    constexpr auto t1 = &c1;
    constexpr auto t2 = &c2;
    static_assert(t1 != t2);
}

Related is bug 70248 where the compiler accepts unspecified equality comparisons as constant expressions when it shouldn't.
Comment 1 Barry Revzin 2021-01-31 16:56:10 UTC
Here's a different, C++20-specific test case:

constexpr bool f() {
    int* a = new int;
    int* b = new int;
    bool result = (a == b);
    delete a;
    delete b;
    return result;
}

static_assert(!f());

Also rejected as non-constant comparison.
Comment 2 Fedor Chelnokov 2021-11-24 07:46:58 UTC
Here is an example without templates that erroneously fails to compile as well:
```
int main() {
    static constexpr int x = 1;
    static constexpr int y = 2;
    static_assert( &x != &y );
}
```
Demo: https://gcc.godbolt.org/z/3WdqP49Gq

Related discussion: https://stackoverflow.com/q/70091821/7325599
Comment 3 Drea Pinski 2021-12-28 10:46:42 UTC
*** Bug 91693 has been marked as a duplicate of this bug. ***
Comment 4 Drea Pinski 2021-12-28 10:48:32 UTC
(In reply to Barry Revzin from comment #1)
> Also rejected as non-constant comparison.

That is a different issue and was fixed for GCC 10.3.0 and GCC 11+.
Comment 5 Drea Pinski 2021-12-28 10:53:40 UTC
For the first testcase, here is a reduced testcase:

template <int> char hh= 0;
static_assert(&hh<0> != &hh<1>, "should not be equal");

----- CUT ----
Note the above is even valid C++14.

And here is the reduced valid C++11 testcase:
template <int> struct a
{
    static const char hh= 0;
};

static_assert(&a<0>::hh!=&a<1>::hh, "");
Comment 6 Patrick Palka 2022-01-06 15:56:15 UTC
(In reply to Andrew Pinski from comment #4)
> (In reply to Barry Revzin from comment #1)
> > Also rejected as non-constant comparison.
> 
> That is a different issue and was fixed for GCC 10.3.0 and GCC 11+.

By r11-7176 FWIW

The other testcases are basically PR94716, and are accepted by GCC 12 since r12-6188