c++ inline functions - mitk.orgcpp... · 2/7/2014 | page 9 t. wiebe mbi summary • inline tells...

12
C++ inline functions

Upload: others

Post on 22-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++ inline functions - mitk.orgcpp... · 2/7/2014 | Page 9 T. Wiebe MBI Summary • Inline tells the compiler to substitute a function by its implementation, in case there is a gain

C++ inline functions

Page 2: C++ inline functions - mitk.orgcpp... · 2/7/2014 | Page 9 T. Wiebe MBI Summary • Inline tells the compiler to substitute a function by its implementation, in case there is a gain

Page 2 2/7/2014 |

T. Wiebe MBI

Outline

1. What is an inline function?

2. How to use inline functions

3. Advantages and disadvantages

4. Summary

Page 3: C++ inline functions - mitk.orgcpp... · 2/7/2014 | Page 9 T. Wiebe MBI Summary • Inline tells the compiler to substitute a function by its implementation, in case there is a gain

Page 3 2/7/2014 |

T. Wiebe MBI

Inline function – What is that?

a command for the compiler to substitute the call for a function by it‘s implementation = inline expansion

Compiler decides if function is substituted or executed

Page 4: C++ inline functions - mitk.orgcpp... · 2/7/2014 | Page 9 T. Wiebe MBI Summary • Inline tells the compiler to substitute a function by its implementation, in case there is a gain

Page 4 2/7/2014 |

T. Wiebe MBI

How to use inline functions

• Example: inline int add(int x, int y)

{

return x+y;

}

int calc()

{

int p =3; int q=5;

int result = add(p,q);

return result;

}

int result = add(p,q);

is substituted by:

int result = p+q;

Page 5: C++ inline functions - mitk.orgcpp... · 2/7/2014 | Page 9 T. Wiebe MBI Summary • Inline tells the compiler to substitute a function by its implementation, in case there is a gain

Page 5 2/7/2014 |

T. Wiebe MBI

How to inline a function (1)

1. Declaration and definition within class declaration function is automatically inline:

//Calculator.h

class Calculator

{

private:

int result;

public:

int getResult() const

{

return result;

}

}

used when function is very short

Page 6: C++ inline functions - mitk.orgcpp... · 2/7/2014 | Page 9 T. Wiebe MBI Summary • Inline tells the compiler to substitute a function by its implementation, in case there is a gain

Page 6 2/7/2014 |

T. Wiebe MBI

How to inline a function (2)

2. Declaration and definition as inline function in header-file: //Calculator.h

class Calculator

{

private:

int result;

public:

int getResult() const;

}

inline int Calculator::GetResult() const

{

return result;

}

Used for longer, but still simple methods (no loops etc.)

Page 7: C++ inline functions - mitk.orgcpp... · 2/7/2014 | Page 9 T. Wiebe MBI Summary • Inline tells the compiler to substitute a function by its implementation, in case there is a gain

Page 7 2/7/2014 |

T. Wiebe MBI

Advantages

• Why should I use it?

Faster than ordinary function call, which goes as follows:

• Put return adress on the stack

• Put parameters on the stack

• Execute function

• Deallocate parameters

• Jump back to starting point

Page 8: C++ inline functions - mitk.orgcpp... · 2/7/2014 | Page 9 T. Wiebe MBI Summary • Inline tells the compiler to substitute a function by its implementation, in case there is a gain

Page 8 2/7/2014 |

T. Wiebe MBI

Disadvantages

• Compiler decides in general itself which functions to inline inline is a recommendation for the compiler

Does not always result in gain in speed

inline declaration is not always necessary

• Gain in speed is often very small, so that inlining is not needed

• inline breaks encapsulation because code is revealed, also the header-file is larger

• When changing an inline function, all the code, which uses the inline function, has to be recompiled

Page 9: C++ inline functions - mitk.orgcpp... · 2/7/2014 | Page 9 T. Wiebe MBI Summary • Inline tells the compiler to substitute a function by its implementation, in case there is a gain

Page 9 2/7/2014 |

T. Wiebe MBI

Summary

• Inline tells the compiler to substitute a function by its implementation, in case there is a gain in speed

• Used for short methods, which are frequently used, because it may be faster than an ordinary function call

• Should be used carefully, because it does not necessarily result in the desired way, because compiler decides itself when to inline a function

Page 10: C++ inline functions - mitk.orgcpp... · 2/7/2014 | Page 9 T. Wiebe MBI Summary • Inline tells the compiler to substitute a function by its implementation, in case there is a gain

Page 10 2/7/2014 |

T. Wiebe MBI

• Any Questions?

Soruce : http://1.bp.blogspot.com/_mbfqNmS63yQ/TAky5GWN2RI/AAAAAAAAA_0/49yXbk2ab_E/s1600/fragezeichen.gif

Page 11: C++ inline functions - mitk.orgcpp... · 2/7/2014 | Page 9 T. Wiebe MBI Summary • Inline tells the compiler to substitute a function by its implementation, in case there is a gain

Page 11 2/7/2014 |

T. Wiebe MBI

Thank you for your attention!

Page 12: C++ inline functions - mitk.orgcpp... · 2/7/2014 | Page 9 T. Wiebe MBI Summary • Inline tells the compiler to substitute a function by its implementation, in case there is a gain

Page 12 2/7/2014 |

T. Wiebe MBI

References

• Wieland, T.; C++-Entwicklung mit Linux, 3. überarb. Auflage, Heidelberg: dpunkt.verlag GmbH, 2004, S. 101 - 105

• Willemer, A.; C++ - der Einstieg, 3. überarb. Auflage, Bonn: Galileo Press, 2007, S. 177 – 178

• Kaiser, R.; C++ mit Microsoft Visual C++ 2008, Berlin, Heidelberg: Springer, 2009, S. 590 - 592