c++ - Using the non-virtual-interface idiom, can/will my non-virtual function be inlined ? -
i decided use non-virtual interface idiom (nvi) design interface in c++, purpose of using parameter default value (thus avoiding problems caused fact default parameters statically bound).
i came rather trivial declaration class, looking :
class interface{ public: void func(parameter p = 0); virtual ~interface(); private: virtual void dofunc(parameter p)=0; }; void interface::func(parameter p){ dofunc(p); } interface::~interface() {} i know providing function body in header automatically mark function candidate inlining (though don't know if placing definition outside of class prevent that). know virtual function aren't inlined obvious reasons (we don't know function called @ runtime, can't replace calls function's body obviously).
then, in case, func() marked candidate inlining ? isn't virtual function, still calls virtual function. make inline-able ?
extra question: worth ? body consist of 1 statement.
note question quite rather learning rather searching optimization everywhere. know function called few time (well, now, might prudent how program evolves), , inlining quite superfluous , not main concern program's performance.
thanks !
i know providing function body in header automatically mark function candidate inlining
more or less; providing function body in class definition, or explicitly declaring inline if it's in header. otherwise, function subject 1 definition rule, , you'll errors if include header in more 1 translation unit.
note doesn't force compiler inline calls function; providing definition in header allows inline in translation unit includes header, if thinks it's worth it. also, compilers can perform "whole program optimisation", , inline functions when definition not available @ call site.
then, in case, func() marked candidate inlining ? isn't virtual function, still calls virtual function. make inline-able ?
yes, functions candidates inlining. if call themselves, couldn't inline calls; nor if function isn't known @ compile time (e.g. because must called virtually, or through function pointer). in case, inlining function replace direct call func() virtual call dofunc().
note virtual calls can inlined, if dynamic type known @ compile time. example:
struct myimplementation : interface {/*whatever*/}; myimplementation thing; thing.func(); // known myimplementation, func , dofunc can inlined extra question: worth ?
that depends on "it" is. if mean compile time then, long function remains short, might benefit (possibly significant, if function called many times) negligible cost. if mean cost of spending time choosing put it, not; put wherever's convenient.
Comments
Post a Comment