I was trying to verify in a static_assert
that a program had really two distinct classes produced from a template by comparing the pointers on their static fields. After some simplifications the program looks as follows:
template<int N> struct C {
static int x;
};
template<int N> int C<N>::x = 0;
int main() {
static_assert(&C<0>::x != &C<1>::x);
}
Clang is ok with it, but GCC prints the error:
error: non-constant condition for static assertion
demo: https://gcc.godbolt.org/z/o6dE3GaMK
I wonder whether this type of check is really allowed to do in compile time?
Compile-time equality comparison of the pointers on static class members is allowed.
The problem with original code was only in GCC and due to old GCC bug 85428.
To workaround it, one can explicitly instantiate the templates as follows:
template<int N> struct C {
static int x;
};
template<int N> int C<N>::x = 0;
template struct C<0>;
template struct C<1>;
int main() {
static_assert(&C<0>::x != &C<1>::x);
}
This program is now accepted by all 3 major compilers, demo: https://gcc.godbolt.org/z/Kss3x8GnW