SHOW / EPISODE

Ep 2 - [C++] What happens when you write an Inline function?

Season 1 | Episode 2
6m | Jun 6, 2021

Inline is a request to the compiler, not a command

 

Inline function is an important concept in C++ and it is a widely asked interview question also.

In this episode, Dheeraj is talking about the Inline function. He talks about how to create an Inline function. What the compiler does when you write an inline function?

 

There is a bonus tip in the episode. So keep listening.

 

www.jhadheeraj.com

LinkedIn: https://www.linkedin.com/in/jhadheeraj

Twitter: https://twitter.com/dheerajjha_03

Instagram: https://www.instagram.com/jha.dheeraj03

-------------------------------------------------------------------------------------------

Transcript:

 

Welcome to Programming Gyan - Tips and Tricks with Dheeraj Jha

Episode2 - [C++] What happens when you write an Inline function?

 

In episode one when I was discussing the macro functions or you can say function-like macros, I suggested you use inline functions instead. I also gave you some brief about the inline function.

 

I thought it’s a good topic to talk about in 2nd episode. In this episode, I will give you some more details of inline functions. When you should use it and when not. I will also tell you how the compiler handles inline functions.

How to write an inline function?

So before we go into details, let's see what are the ways to define inline function.

when you define a non-class member inline function,

  • The Declaration of an inline function is like a normal function.
  • you prepend the function’s definition with the keyword inline, and you put the definition into a header file.

 

You can also define a class member inline function. Steps are same

  • The declaration is the same as other member functions.
  • You will have to add an inline keyword before the definition of your inline function.
  • And define your function outside the class body but in the header file.

 

There is another way to create a class member inline function.

  • You define your function within your class definition where you are declaring your function.
  • No need to use the Inline prefix.

 

I will ask you a question here.

Do you have any idea Why you should define your inline function in the header file?

It's fine if you don't know the answer and I will answer this question in the end.

 

But what actually happens when you define an Inline function?

 

When you define a function, it gets stored in the code segment of your application.

 

 Now when you call this function in your program, your CPU loads this function executes and returns to the place from where this function gets called, to the caller.

 

When you define a function as inline, the compiler adds the function definition where the function is getting called. By doing this you save the cost of a function call.

 

In real life, there is no free lunch. Inline functions are also not exceptions. Inline functions save the function call costs by replacing the function call with the function body. I hope you must have observed here that it will increase the object code size.

 

 

And if in excitement you write lots of inline functions, it's not good for the machines which run on low or limited memory or many embedded systems.

 

Your compiler does all this for you. But remember this, Inline is a request to the compiler, not a command. Adding the Inline keyword doesn't guarantee this optimization.

The compiler can ignore the request for inline. The compiler may not perform inline in such circumstances like:

  • If a function contains a loop, switch, or goto statement or it contains static variables.
  • The compiler ignores recursive functions for this optimization.
  • Virtual functions defy inlining. This should not surprise as virtual means "wait till runtime" and inline is "before execution".

 

Remember sometimes compiler generates a function body for a function that is a perfect candidate for inlining. Now the question is why? I can't give you all the answers here but will give you some idea.

Consider you have a function pointer that is pointing to your function, which is a good candidate for inlining. And also you added the Inline keyword. Now because your function is inline(assume compiler did required optimizations), it will not get memory in the code segment. Now think about what will happen when you call your function using a function pointer.

Intelligently compiler refuses your request in such cases and saves you.

 

I hope till now you are with me and understood how Inline works.

 

Now the question comes to do Inline functions improve performance?

There is no straight yes or no. You will have to play with it to see what suits you best in your case.

 

Do not stick to any answer like "Never use" or "Always use Inline function" or someone says "Use inline functions if it is less than N number of lines." You can use these as guidelines but don't rely on them.

 

Now coming back to the question, I asked you Do you have an idea Why you should define your inline function in the header file?

 

Inline functions should be in header files because most build environments do inlining during compilation. To replace the function call with the function body, the compiler needs to know how the function looks like.

 

If you put the inline function’s definition into a .cpp file and you call it from some other .cpp file, you’ll get an “unresolved external” error from the linker. You can still provide the definition in the .cpp file till you use that function in the same file.

 

I hope, you got something new from this episode. Subscribe to this podcast and follow me on LinkedIn, Twitter, and Instagram. I will add links in the description. To know more about me you can visit www.jhadheeraj.com

 

I will meet you in the next episode with some other interesting tips and tricks.

 

Thank you for listening ProgrammingGyan-Tips and Tricks with Dheeraj Jha.

 

Audio Player Image
Programmin Gyan
Loading...