Lin*_*gxi 5 c++ casting pointer-to-member thunk language-lawyer
以下源代码在 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)();
}
Run Code Online (Sandbox Code Playgroud)
生成的代码不正确,因为pA调用时指针没有调整pa,导致中的this指针值错误f。但是,代码在 GCC 和 clang 中编译得很好,没有任何警告(严格别名除外)。指针pA在 GCC 和 clang 生成的代码中进行了适当的调整。所以,我想知道标准对此有何看法?上面代码中的演员是否符合标准?还是 GCC 和 clang 的非标准扩展?
| 归档时间: |
|
| 查看次数: |
154 次 |
| 最近记录: |