Vote count: 0
Consider the following (oversimplified) situation:
class Base is a base class with pure virtual function without virtual destructor (since it's unnecessary for its intended usage).
struct A is a wrapper around function pointer, namely
struct A {
ret_t (*f)(base&);
/* ... */
};
ret_t f1(base&); // there are corresponding functions
ret_t f2(base&);
ret_t f3(base&);
where ret_t is some arbitrary type.
And there are functions whose signatures are like ret_t call(A a). For instance:
ret_t call(A a){
derived d;
return a.f(d);
}
where derived is derived from base, and there are multiple derived class from base.
So far, so good.
However, since a.f is called through function pointer, the call to base virtual functions cannot be devirtualized. That being said, the derived class is local to the function call, and thus can be devirutalized eventually theoretically.
That aside, I'm pretty sure, at runtime, only one derived class would be extensively used, and others are unlikely to be called. And I have seen that g++ produce code that do a cmp on function pointer if there's only one derived class.
So I want g++ to emit code that devirtualize at the final functions f1, f2, f3, or I want to specify that a special virtual function to test against, and thereby optimize more on that.
How to force compiler emits devirtualized code
Aucun commentaire:
Enregistrer un commentaire