I always thought that when I converted a member function ptr in C to a member function ptr in A, I just had to make sure that the call was pointing to an C entity , but that was a mistake. Does the standard describe this use case in more detail?(checked with multiple compilers)
struct A
{
char i[7];
void test1(){
std::cout << __LINE__;
}
};
struct B
{
char i[14];
virtual void test(){
std::cout << __LINE__;
}
};
struct C:A,B
{
char i[31];
};
using T = void (C::*)();
int main()
{
C c;
(c.*T(&B::test))();//good
(c.*static_cast<decltype(&A::test1)>(T(&B::test)))();//crash
return 0;
}
If that’s the case, why do member function pointers for polymorphic classes require two pointers’ worth of space? Can’t the offset of vtable_ptr be computed through the type?”
9