https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114031
Bug ID: 114031
Summary: gcc rejects valid code with pointer to member field
cast inside a class not completed
Product: gcc
Version: 13.2.0
Status: UNCONFIRMED
Keywords: diagnostic, rejects-valid
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: rush102333 at gmail dot com
Target Milestone: ---
The following code is rejected by gcc 13 ,which does not trigger any error in
both clang and MSVC:
https://godbolt.org/z/5T7sx8vs8:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<iostream>
struct A { int a; };
struct B:A {
int y=0;
constexpr static bool b=(int(A::*))&B::y;
};
int main(){
std::cout<<B::b<<std::endl;
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The compiler complains that &B::y can't be evaluated as constant when class B
is still incomplete:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:5:42: error: '&B::y' is not a constant expression when the class 'B'
is still incomplete
5 | constexpr static bool b=(int(A::*))&B::y;
| ^
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Note that merely using &B::y without the member pointer conversion
expression(int (A:: *)) does not trigger any error:
https://godbolt.org/z/7oErdvKv8.
If it's &B::y that indeed causes the error, that shouldn't compile either.
This looks somewhat similar to PR107574. At least the error message needs
further improvement.