Page 1 of 1

Inline Functions what they are, how to use them Rate Topic: -----

#1 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Posted 19 January 2010 - 10:40 AM

*
POPULAR

Introduction
In C, one of the ways to preserve efficiency is through the use of macros, which allows you to make what looks like a function without the normal function call's overhead. Macros are replaced with macro codes by preprocessor.
There are some problems here, with using macro: First, No type checking is done when replacing macros. Second, macros cannot access private class members. So... what happens to the efficiency I said ? This was solved by bringing in the concept of inline functions...

Syntax
Making a C++ function inline is so easy, the function definition is the same as normal functions, just add the inline keyword before the return type. This simple function is defined as inline:
inline void func() {
	cout<<"inline function"<<endl;
}

Now, we are going to explain how to implement inline functions inside classes... Look at this very simple class:
class simple {
	int val;
public:
	int ret();
	void set(const int& a);
};

int simple::ret() {
	return val;
}

void simple::set(const int& a) {
	val = a;
}

In this class, there are two very basic member functions named ret and set, the first one returns the value of the internal variable and the second one assigns the internal value a given argument. Such little/simple functions should be defined as inline to avoid having function call overhead for them... which helps improve the speed and efficiency of our class.

How do we define member functions as inline? the answer is: Just define them right inside the class body; all functions defined inside class' body are treated as inline functions... how ever you Can add the inline keyword to the function definition also, but why not make it shorter? :D
Look at the updated class:
class simple {
	int val;
public:
	//inline function
	int ret() {
		return val;
	}
	//inline function
	void set(const int& a) {
		val = a;
	}
};

So, now these two member functions are inline. If you want to keep the class interface clean, you can have the same normal prototype in class interface and have the inline keyword in definition:
class simple {
	int val;
public:
	int ret();
	void set(const int& a);
};

inline int simple::ret() {
	return val;
}

inline void simple::set(const int& a) {
	val = a;
}

Just note that, The inline code Does occupy space, but if the function is small, this can actually take less space than the code generated to do an ordinary function call.
OK, now speak about the problems and limitations...

A Compiler can only make a function inline if the definition of the function exits within the same translation unit as the call, which means inside the same source/header file it was defined, plus header files included inside this file.
Also note that, by putting the inline keyword (or by other ways mentioned), there is no guarantee that they will be made inline, as it is Only a Suggestion to the compiler... after this suggestion, the compiler decides whether or not it can go inline. Always note that, very simple functions that have no loops(looping is counted as a complicated operation for replacement) should be defined as inline to give you efficiency. But as mentioned, it is a suggestion and the compiler is the final decider. *did you notice I said that note two times in a row? because it was important :D*

Hope this tutorial helped you understand usage of inline and benefits it can give you :)
Special Thanks to NickDMax for mentioning the problems and weak points of this article.


Is This A Good Question/Topic? 5
  • +

Replies To: Inline Functions

#2 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Posted 21 January 2010 - 02:01 PM

Great tutorial :^: I didn't know that putting a definition inside a class made it inline :D Now I'll remember this
Was This Post Helpful? 0
  • +
  • -

#3 TMKCodes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 48
  • View blog
  • Posts: 440
  • Joined: 21-March 09

Posted 29 January 2010 - 06:03 AM

Very nice tutorial. I did not have much idea what inline function is even tough i've been coding C++ for few years, but now i do. Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Posted 10 March 2010 - 10:22 PM

A good use for inlines is to avoid the stack frame preservation checks done by the compiler for a regular function.
Was This Post Helpful? 1
  • +
  • -

#5 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 125
  • View blog
  • Posts: 555
  • Joined: 30-October 10

Posted 08 May 2011 - 05:24 AM

Thanks for the excellent tutorial mate, ;) I will definitely find this feature very useful for the small programs I write to solve maths problems. I regularly isolate small operations inside functions for the sake of clarity e.g.
bool IsPentagonal(int x)
{
	return fmod((sqrt(24.0 * x + 1) + 1) / 6.0, 1) == 0 ? true : false;
}

Now I can get the best of both worlds! ^^
Was This Post Helpful? 0
  • +
  • -

#6 Xploit_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-August 11

Posted 05 August 2011 - 05:29 PM

Could someone explain why it would be bad to try and make every function inline? I mean the compiler decides which is better anyway right?
also, how could you make inline functions and still have them implemented in separate files
Was This Post Helpful? 0
  • +
  • -

#7 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Posted 14 August 2011 - 12:34 AM

Quote

also, how could you make inline functions and still have them implemented in separate files
Simply use the keyword inline before them e.g.
// header
void foo ();
// implementation
inline
void foo () {}

Quote

Could someone explain why it would be bad to try and make every function inline? I mean the compiler decides which is better anyway right?
Because the compiler isn't always right. Inlining large functions that are used multiple times balloons executable size.

Non-inline functions occur only once in your executable file. However, inline means it can occur multiple times throughout the file. For large functions this can be a problem.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1