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.
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.
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
*** Bug 91693 has been marked as a duplicate of this bug. ***
(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+.
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, "");
(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