Reputation: 1150
I have the below code :
static inline void print(const std::string& value)
{
std::cout<< value <<std::endl;
}
Is the above function really forcing compiler to replace it multiple places? I want to know whether is it really helpful?
Since this meaning of the keyword inline is non-binding, compilers are free to use inline substitution for any function that's not marked inline, and are free to generate function calls to any function marked inline. Those optimization choices do not change the rules regarding multiple definitions and shared statics listed above.
Upvotes: 0
Views: 64
Reputation: 36896
The compiler's optimizer is complex and will use heuristics to decide whether to inline code. There are many factors involved in that decision, and use of inline
is probably considered. There exist things like MSVC's __forceinline
which may give a bigger nudge to that decision but will still not guarantee it.
But in general you can trust the compiler to make the right decision, and functionally there will be no difference to you. You can use recursion, take the address of that function, and the compiler will make it work, inline or not.
The biggest practical difference is what @arnes said:
it permits the functions defined multiple times in different translation units but compiler expects all the definitions are same (otherwise causes undefined behaviour)
Upvotes: 2