inline - c++ function inling decided at compile time -


it said function can inlined if known @ compile time how called, or effect. (pls correct/clarify if wrong)

so if have function this

void calling() { if (m_inputstate == state::fast) //m_inputstate class member , set user { callfastfunction(); }     else if (m_inputstate == state::slow) { callslowfunction(); } } 

as m_inputstate set end-user, can variable not known @ compile time hence calling() cant inlined?

the compiler can (and will) inline calling function. depending on availability of source of callfastfunction , callslowfunction, these may inlined.

if compiler can determine value of m_inputstate, remove if - if it's definitive value 1 value.

for example, thing.m_inputstate = state::slow; thing.calling(); compile in "slow" call, without conditonal code, std::cin >> thing.m_inputstate; thing.calling(); of course not.

if, through profile-based optimisation, compiler knows how each of cases chosen, may select path ends "next" in order of code, such comes first (and may provide prefix or other indication processor "you're going way".

but inlining happens based on:

  1. the code being available.
  2. the size , number of calls function.
  3. arbitrary compiler decisions can't know (unless we're familiar source-code of compiler).

modern compilers support "link time optimisation", object files produced "half-done", such linker produce final code, , can move code around , inline, same old-fashioned compiler, on entire code makes executable (all code using link-time optimisation), allowing code didn't have same source file or not in header file still inlined.


Comments