http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52902
Bug #: 52902
Summary: Pointer to member rejected in constant expression
Classification: Unclassified
Product: gcc
Version: 4.7.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: [email protected]
ReportedBy: [email protected]
#########################################################################
struct X {
int a;
};
constexpr int X::* mem = &X::a;
void foo()
{
constexpr X x = {1};
constexpr int k = x.a; // ok
constexpr int l = x.*mem; // should be ok <------------------
//constexpr int const* m = &(x.a); // legitimate error
}
#########################################################################
g++-4.7 -std=c++0x bug.cpp -o g47.out
bug.cpp: In function ‘void foo()’:
bug.cpp:29:24: error: ‘(const int*)(& x)’ is not a constant expression
#########################################################################
As far as I can see, a pointer to member access should be a constant
expresssion, iff both the object and the pointer are constant expressions.
This behaviour is shown by the clang++ compiler, which does not emit the above
error.
It seems gcc evalutates the expression
x.*mem
as something like
*((const int*)(& x) + mem)
In this implementation, the left (intermediate) operand to the + operator is
not a constant expression, that's why gcc rejects it by mistake.