What is Inline declaration?
-
Hi All,
I am following the book C++ GUI Programming with Qt.
In chapter 4, section -subclassing QTable Widget , I came across a statement,
The autoRecalculate() function is implemented inline since it just returns whether or not auto-recalculation is in force.
What is meant by implemented inline ?
From this site : webeduclick.com
"An inline function is a function that is expanded in line when it is invoked, the compiler replaces the function call with the corresponding function code."
What's the specialty of inline function? Aren't all functions replaced by their logic in their call statement?
Please clarify doubt!
Thanks!
-
@Swati777999 to expand a little bit on what the others wrote.
calling functions is not free, its very cheap, but its not free.
Generally speaking, when calling a function a couple of operations have to be done, before the actual instructions of your function are started to be executed.
- Preparing parameters for the function
- Preparing the return value of the function
- Prologue and epilogue code, local memory management, parameter memory management and register value preservation, jumping to the different memory address where your function instruction is stored, etc
- clearing of already cached instructions
All this "takes time", relatively speaking.
by inlining you remove that overhead and trade speed for size.
Keep in mind, that in c++ the inline keyword is a suggestion from you to your compiler, the compiler may very well ignore it or do inlining by itself
for more information:
https://en.cppreference.com/w/cpp/language/inline -
It is as written. "inline" explicitly tells the compiler to replce the call statement with the function/method body where it is called. in tight iterative loops this can increase performance over the normal subroutine call when not inlined. As a generality get/set operations are good to inline since they are often simple assignments, so are shorter than the the indirect subroutine call just to then do the assignment.
-
@Swati777999 to expand a little bit on what the others wrote.
calling functions is not free, its very cheap, but its not free.
Generally speaking, when calling a function a couple of operations have to be done, before the actual instructions of your function are started to be executed.
- Preparing parameters for the function
- Preparing the return value of the function
- Prologue and epilogue code, local memory management, parameter memory management and register value preservation, jumping to the different memory address where your function instruction is stored, etc
- clearing of already cached instructions
All this "takes time", relatively speaking.
by inlining you remove that overhead and trade speed for size.
Keep in mind, that in c++ the inline keyword is a suggestion from you to your compiler, the compiler may very well ignore it or do inlining by itself
for more information:
https://en.cppreference.com/w/cpp/language/inline -
@J-Hilk said in What is Inline declaration?:
for more information:
https://en.cppreference.com/w/cpp/language/inlineThis link has better examples. Thanks for sharing!