Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline functions in c++ - conditions

Tags:

c++

Under What condition an inline function ceases to be an inline function and acts as any other function?

like image 425
Amol Avatar asked Jun 02 '12 10:06

Amol


2 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 inline function ceases to be an inline function 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 ).

like image 62
Alok Save Avatar answered Sep 19 '22 10:09

Alok Save


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)
like image 22
Mesop Avatar answered Sep 21 '22 10:09

Mesop