top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

At what stage of compilation inline function is replaced with fn call & in what conditions inline function will fail?

+5 votes
1,084 views

I have few queries about inline function, can anyone please help?

  1. In what stage of compilation inline function is replaced with function call?
  2. Under what conditions inline function will fail?
  3. How to verify whether inline function is successfully replaced or not?
posted Apr 25, 2016 by Chirag Gangdev

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

Inline functions are not always important, but it is good to understand them. The basic idea is to save time at a cost in space. Inline functions are a lot like a placeholder. Once you define an inline function, using the 'inline' keyword, whenever you call that function the compiler will replace the function call with the actual code from the function.

A WORD OF WARNING: Inline functions are very good for saving time, but if you use them too often or with large functions you will have a tremendously large program. Sometimes large programs are actually less efficient, and therefore they will run more slowly than before. Inline functions are best for small functions that are called often.

Finally, note that the compiler may choose, in its infinite wisdom, to ignore your attempt to inline a function. So if you do make a mistake and inline a monster fifty-line function that gets called thousands of times, the compiler may ignore you.

answer Apr 25, 2016 by Siddalingkumar
Thank you,
I know it is on compiler whether to consider our inline request or to ignore it,
What i am asking is, there are some conditions, as you said if function is very long, or lets say if it is a recursion function then it will fail, Right?
So i am asking is for which other conditions it will fail? Can you help?
+2 votes

Most of the things are cleared by SiddalingKumar - Just adding few points
1. How to verify whether inline function is successfully replaced or not?
This substitution is performed by the C++ compiler at compile time. When the compiler inline-expands a function call, the function's code gets inserted into the caller's code stream (conceptually similar to what happens with a #define macro)

Point no 2 has been discussed by SiddalingKumar in detail, the only thing which I want to add is most compilers do automatically inline functions, it's best to declare inline just to describe intent.

Point no 3. No idea...

answer Apr 25, 2016 by Salil Agrawal
Thank you Sir,
Generally in pre-processor stage of compilation macro and header file will be included, So, to Check whether inline function also gets replaced in pre-processor stage or not, I have compiled a CPP code with '-E' option and created a file with '.i'
And i found that it is not replaced. in pre-processor stage.
Please note that function was only 1 line code and very very simple code
Inline function is not pre-processor (precompilation) processing. Its at the compile time.
Similar Questions
+2 votes

As per my understanding, inline function will be copied/replaced at function call. so my question is on which stage of compilation it is done?

0 votes

Which is the best practice for variable declaration within the function ? Or is it vary from one language to other one ?

+1 vote

1)I have an inline function with recursion, when i compiled it i am not seeing any Error/Warning for that why?
2)As far as i know its compiler dependent whether to copy inline function or not, but my question is how we will get to know whether compiler has added inline function or not?
3)In What stage of compilation Inline function will be copied?

...