Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline keyword vs header definition

What's the difference between using the inline keyword before a function and just declaring the whole function in the header?

so...

int whatever() { return 4; } 

vs

.h:

inline int whatever(); 

.cpp:

inline int myClass::whatever() {     return 4; } 

for that matter, what does this do:

inline int whatever() { return 4; } 
like image 910
Dollarslice Avatar asked Apr 11 '12 09:04

Dollarslice


People also ask

Why are inline functions defined in headers?

The definition of an inline function doesn't have to be in a header file but, because of the one definition rule (ODR) for inline functions, an identical definition for the function must exist in every translation unit that uses it. The easiest way to achieve this is by putting the definition in a header file.

What is inline keyword?

Answer. An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

What does inline keyword mean in C++?

With inline keyword, the compiler replaces the function call statement with the function code itself (process called expansion) and then compiles the entire code.


2 Answers

There are several facets:

Language

  • When a function is marked with the inline keyword, then its definition should be available in the TU or the program is ill-formed.
  • Any function defined right in the class definition is implicitly marked inline.
  • A function marked inline (implicitly or explicitly) may be defined in several TUs (respecting the ODR), whereas it is not the case for regular functions.
  • Template functions (not fully specialized) get the same treatment as inline ones.

Compiler behavior

  • A function marked inline will be emitted as a weak symbol in each object file where it is necessary, this may increase their size (look up template bloat).
  • Whereas the compiler actually inlines the call (ie, copy/paste the code at the point of use instead of performing a regular function call) is entirely at the compiler's discretion. The presence of the keyword may, or not, influence the decision but it is, at best, a hint.

Linker behavior

  • Weak symbols are merged together to have a single occurrence in the final library. A good linker could check that the multiple definitions concur but this is not required.
like image 175
Matthieu M. Avatar answered Oct 16 '22 04:10

Matthieu M.


without inline, you will likely end up with multiple exported symbols, if the function is declared at the namespace or global scope (results in linker errors).

however, for a class (as seen in your example), most compilers implicitly declare the method as inline (-fno-default-inline will disable that default on GCC).

if you declare a function as inline, the compiler may expect to see its definition in the translation. therefore, you should reserve it for the times the definition is visible.

at a higher level: a definition in the class declaration is frequently visible to more translations. this can result in better optimization, and it can result in increased compile times.

unless hand optimization and fast compiles are both important, it's unusual to use the keyword in a class declaration these days.

like image 33
justin Avatar answered Oct 16 '22 03:10

justin