Under What condition an inline function ceases to be an inline function and acts as any other function?
3 Answers
The Myth:
inline is just a suggestion which a compiler may or may not abide to. A good compiler will anyways do what needs to be done.
The Truth:
inline usually indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules(especially w.r.t One Definition Rule) for inline are followed.
Under What condition an
inlinefunction ceases to be aninlinefunction and acts as any other function?
Given the quoted fact there is a deeper context to this question.
When you declare a function as static inline function, the function acts like any other static function and the keyword inline has no importance anymore, it becomes redundant.
The static keyword on the function forces the inline function to have an internal linkage.(inline functions have external linkage) Each instance of such a function is treated as a separate function(address of each function is different) and each instance of these functions have their own copies of static local variables & string literals(an inline function has only one copy of these ).
4 Comments
inline is much more than just a suggestion. It has even more important effects when use without static. When used without static the exceptions to the ODR which it enables have important effects; with static they become (almost) irrelevant.static inline function behaves like a normal static function and will have a copy in each TU and hence an unique address in each TU and inline has no effect anymore.I think my answer just says that.I was answering the Q "Under What condition an inline function ceases to be an inline function and acts as any other function?" the condition is that if it is static inline. or is there something that I missed?inline static function acts like any normal static function, but that in general an inline function doesn't act like any normal function? If so, then yes, I agree. The fact that your second paragraph only applies to static functions wasn't at all obvious to me.It's at the discretion of the compiler.
But some cases just can't be inlined, like:
- Recursive functions
- Functions whose address is referenced somewhere
- Virtual functions (there are some exceptions thought)
1 Comment
That depends on the compiler optimization.
Different compilers have different rules to make the code more efficient. But if you declare a function as inline, the compiler tends to respect your decision as long as none of it's rules says different.
Remember that the compiler can change completely the execution path of a method. For example, consider the next situation:
int MyClass::getValue() { return someVariable; } At compile time, there is little difference between declaring this kind of function as inline or not. Probably the compiler will make the attribute partially public and code:
myInstance->getValue() as
myInstance->someVariable So it's more an aesthetical decision in most cases.