Hard
A virtual call before and after placement new
morph lives in its own translation unit, so it is opaque to twice. Built with Clang at -O2 the program prints 12; adding -fstrict-vtable-pointers, it prints 11. Which statement is correct?
main.cpp
#include <cstdio>
#include <new>
struct Base { virtual int id() { return 1; } };
struct Derived : Base { int id() override { return 2; } };
// morph.cpp (separate TU)
void morph(Base* b) {
b->~Base();
new (b) Derived; // same size, same alignment
}
// main.cpp
void morph(Base* b);
int twice(Base* b) {
int first = b->id();
morph(b);
int second = b->id(); // (1)
return first * 10 + second;
}
int main() {
std::printf("%d\n", twice(new Base));
}33 of 44 in Compilers