c++ - Inlining of virtual function when I can specify the actual type of the object as a template parameter -
i have virtual class calculator , example implementation mycalculator of class.
template <class t> class calculator{ virtual void op1() = 0; }; template <class t> class mycalculator : public calculator{ void op1(){ do_something(); } }; when use op1() demonstrated in following function, compiler can of course not inline op1() since virtual:
void calculate(calculator* calc){ calc->op1(); } in cases, however, know actual type of calc , d inline performance reasons. think following idea:
template <class c> void calculate(c* calc){ calc->op1(); } i call function follows:
calculator c1 = new calculator(); calculate(c1); // no inling possible in calculate(...) mycalculator c2 = new mycalculator(); calculate(c2); // inlining possible in calculate(...) ? in first example, inlining not possible, think in second example, op1() of mycalculator should inlined inside calculate().
is assumption true?
you can mark virtual function final in c++11 means cannot override in more derived class. compiler can inline function. otherwise compiler cannot assured not calling more derived type different override, therefore cannot inline.
Comments
Post a Comment