Reputation: 189
Given that marking a function inline is only a request to the compiler, I am wondering if some compilers may produce multiple definition errors (if the inline request is rejected) whereas other compilers may not (if the inline request is accepted)?
If so, does that make it preferable to mark the free function as static rather than inline since this will guarantee that each TU has its own copy of the function and avoid any compiler specific issues?
Thanks in advance!
Upvotes: 1
Views: 622
Reputation: 206546
inline
being suggestion is only true for the part of replacing function call with actual definition. inline
is the only way to safely bypass the One definition rule in a standard approved way. But it requires all function definitions to be identical.
Good Read: A previous answer of mine:
Should I define static inline methods in header file?
Upvotes: 1
Reputation: 254501
It's not "only a request" - the effect on linkage, relaxing the One Definition Rule to allow identical definitions in multiple translation units, is well defined and portable.
It's only a "request" in the sense that it's up to the compiler whether to inline calls to any function, regardless of whether they're declared inline
. As with nearly all optimisations, the "as if" rule applies - the programs behaviour, and correctness, mustn't depend on whether or not the optimisation was applied.
Declaring it static
is not a good idea. At best, it will bloat the program with multiple, identical functions; at worst, it will break code that relies on the function having a consistent address, or shared static variables, between translation units.
Upvotes: 2