I need to know how does this thing work? By this i mean ” all calls to an inline function must be recompiled”. I am reading a book which says that each time the inline function is used in our program, the compiler will recompile the short function definition and place a copy of this compiled short definition in your code.
I don’t understand this at all. An explanation with example showing the whole proces will be highly appreciated. Also, could you please explain how does it improve the efficiency.
Thanks a lot.
Suppose you have this code:
If
foois inlined, then the compiler will essentially rewrite the code as:Now if instead of
bar();you had a more complex piece of code, then that piece of code would appear (and be compiled) three times, rather than just once.You trade the cost of a function call against increased and repetitive code.
The compiler may well refuse to actually inline a function. If you ever take the address of
fooand pass it somewhere outside, you cannot even get around creating a stand-alone version. In practice, a happy mix of standalones and inlining will happen, depending on what fits best. (More important to what code ends up being generated is the effect of theinlinekeyword on the one-definition rule, though.)