I was told by a friend that compiler inline is better than the manual inline but he could not explain me why. What is the benefit of using the inline keyword if one can do it manually (except for writing same piece of code again and again)?
Share
Inlineworks like a copy/paste controlled by the compiler, which is quite different from a pre-processor macro: The macro will be forcibly inlined, will pollute all the namespaces and code, won’t be easily debuggable, and will be done even if the compiler would have ruled it as inefficient.By marking it as
inline, you can put a function definition in a header file (i.e. it can be included in multiple compilation unit, without the linker complaining).Marking something
inlinedoes not give you a guarantee that it will be inline. It’s just a suggestion to the compiler. Sometimes it’s not possible such as when you have a virtual function, or when there is recursion involved. And sometimes the compiler just chooses not to use it.