Bug 91693 - Comparing addresses of templated inline vars gets bogus "not a constant expression" error
Summary: Comparing addresses of templated inline vars gets bogus "not a constant expre...
Status: RESOLVED DUPLICATE of bug 85428
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 9.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
: 94716 (view as bug list)
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2019-09-06 20:58 UTC by Jorg Brown
Modified: 2021-12-28 10:46 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-01-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jorg Brown 2019-09-06 20:58:03 UTC
This code works just fine:

inline int inline1 = 0;
inline int inline2 = 0;
static_assert(&inline1 == &inline1);
static_assert(&inline1 != &inline2);

But this code doesn't:

template<typename T>
inline int NullT = 0;

struct A1;
struct A2;

static_assert(&NullT<A1> == &NullT<A1>);
static_assert(&NullT<A1> != &NullT<A2>);

Specifically, it produces this incorrect error:

<source>:13:26: error: non-constant condition for static assertion

   13 | static_assert(&NullT<A1> != &NullT<A2>);

      |               ~~~~~~~~~~~^~~~~~~~~~~~~

<source>:13:26: error: '((& NullT<A1>) != (& NullT<A2>))' is not a constant expression

= = =

Problem reproduces in gcc 7.1 through 9.2; this code works in clang (tested as far back as 5.0) as well as MSVC (tested as far back as v19.14)

See https://godbolt.org/z/njyH5d
Comment 1 Jorg Brown 2019-09-06 21:45:49 UTC
Related missed-optimization bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43565
Comment 2 Martin Liška 2020-01-29 12:10:26 UTC
Confirmed. Clang accepts the code.
Comment 3 Andrew Pinski 2021-12-28 10:39:48 UTC
The inline part of the template variable does not make a difference.
Reduced testcase:

template <int> char hh= 0;
static_assert(&hh<0> != &hh<1>, "should not be equal");
Comment 4 Andrew Pinski 2021-12-28 10:40:00 UTC
*** Bug 94716 has been marked as a duplicate of this bug. ***
Comment 5 Andrew Pinski 2021-12-28 10:46:42 UTC
Dup of bug 85428.

*** This bug has been marked as a duplicate of bug 85428 ***