Clang behaves somewhat weirdly with pointer to member field cast inside a class not completed and inheritance
The following code compiles normally in clang:
struct A { int i; };
struct B:A { int j; };
struct C:B { int k; static_assert((int(B::*))&C::k, ""); };
But it's rejected by gcc which complains that &C::k should not be constant before the class C is completed:
<source>:3:35: error: non-constant condition for static assertion
3 | struct C:B { int k; static_assert((int(B::*))&C::k, ""); };
| ^~~~~~~~~~~~~~~~
<source>:3:35: error: '&C::k' is not a constant expression when the class 'C' is still incomplete
Compiler returned: 1
Note that the static_assert expression requires its first parameter to be compile-time constant. It seems at least one of the compilers does the wrong thing. Since the standard of class member variable pointer assignment looks a little complex, we currently cannot judge for certain which behavior is reasonable. We hope that can be further identified.
@llvm/issue-subscribers-clang-frontend
Author: None (Rush10233)
struct A { int i; };
struct B:A { int j; };
struct C:B { int k; static_assert((int(B::*))&C::k, ""); };
But it's rejected by gcc which complains that &C::k should not be constant before the class C is completed:
<source>:3:35: error: non-constant condition for static assertion
3 | struct C:B { int k; static_assert((int(B::*))&C::k, ""); };
| ^~~~~~~~~~~~~~~~
<source>:3:35: error: '&C::k' is not a constant expression when the class 'C' is still incomplete
Compiler returned: 1
Note that the static_assert expression requires its first parameter to be compile-time constant. It seems at least one of the compilers does the wrong thing. Since the standard of class member variable pointer assignment looks a little complex, we currently cannot judge for certain which behavior is reasonable. We hope that can be further identified.
If we change it to C::* then all implementations accept: https://godbolt.org/z/Tz97rosKj
Note if we change the order of the static_assert and field: https://godbolt.org/z/G94cT7E4s
struct A { int i; };
struct B:A { int j; };
struct C:B {
static_assert((int(B::*))&C::k, "");
int k=0;
};
Implementation agree, but this is IFNDR b/c of: basic.lookup.general p6
I think gcc is wrong on this one.
CC @zygoloid