以下源代码在 VC 中生成警告 C4407,编译器确实生成了不正确的代码。
struct A1 {
int a1;
};
struct A2 {
int a2;
};
struct B: A1, A2 {
void f() {
std::cout << this << '\n';
}
};
int main() {
B b = B();
void (B::*pb)() = &B::f;
void (A2::*pa)() = (void (A2::*)())pb; // performs static_cast actually
std::cout << (std::uintptr_t&)pb << '\n';
std::cout << (std::uintptr_t&)pa << '\n';
B* pB = &b;
A2* pA = pB;
std::cout << pB << '\n';
std::cout << pA << '\n';
(pB->*pb)();
(pA->*pa)();
}
产生的代码是不正确的,因为调用时指针没有pA
调整pa
,导致. 但是,代码在 GCC 和 clang 中编译良好,没有任何警告(严格别名除外)。指针在 GCC 和 clang 生成的代码中进行了适当的调整。所以,我想知道标准对此有何评论?上述代码中的演员表是否符合标准?还是 GCC 和 clang 的非标准扩展?this
f
pA