#include <cstdio>
#include <cstdlib>
struct Interface {
virtual void f() = 0;
};
struct Impl1: Interface {
void f() override {
std::puts("foo");
}
};
// or __attribute__ ((visibility ("hidden")))/anonymous namespace
static Interface* const ptr = new Impl1 ;
int main() {
ptr->f();
}
When compiled with g++-7 -O3 -flto -fdevirtualize-at-ltrans -fipa-pta -fuse-linker-plugin
, the above ptr->f()
call cannot be devirtualized.
It seems that no external library can modify ptr
. Is this a deficiency of GCC optimizer, or because some other sources make devirtualization unavailable in this case?
UPDATE:
It seems that clang-7 with -flto -O3 -fwhole-program-vtables -fvisibility=hidden
is the only compiler+flags (as in 2018/03) that can devirtualize this program.