I know that inline function are either replaced where it is called or behave as a normal function.
But how will I know whether inline function is actually replaced a
You can use tools for listing symbols from object files such as nm on Linux. If the function was inlined, it will not be listed in nm
output - it became part of some other function. Also you will not be able to put breakpoint on this function by name in debugger.
Above answer are very mush useful, I am just adding some point which we keep in our mind while writing inline function.
Remember, inlining is only a request to the compiler, not a command. Compiler can ignore the request for inlining. Compiler may not perform inlining in such circumstances like:
1) If a function contains a loop. (for, while, do-while)
2) If a function contains static variables.
3) If a function is recursive.
4) If a function return type is other than void, and the return statement doesn’t exist in function body.
5) If a function contains switch or goto statement.
Complete info: https://www.geeksforgeeks.org/inline-functions-cpp/
Programatically at run-time, You cannot.
And the truth of the matter is: You don't need to know
An compiler can choose to inline
functions that are not marked inline
or ignore functions marked explicitly inline
, it is completely the wish(read wisdom) of the compiler & You should trust the compiler do its job judiciously. Most of the mainstream compilers will do their job nicely.
If your question is purely from a academic point of view then there are a couple of options available:
You can check the assembly code to check if the function code is inlined at point of calling.
How to generate the assembly code?
For gcc:
Use the -S
switch while compilation.
For ex:
g++ -S FileName.cpp
The generated assembly code is created as file FileName.s
.
For MSVC:
Use the /FA Switch from command line.
In the generated assembly code lookup if there is a call
assembly instruction for the particular function.
Some compilers will emit a warning if they fail to comply an inline function request.
For example, in gcc, the -Winline
command option will emit a warning if the compiler does not inline a function that was declared inline.
Check the GCC documentation for more detail:
-Winline
Warn if a function that is declared as inline cannot be inlined. Even with this option, the compiler does not warn about failures to inline functions declared in system headers.
The compiler uses a variety of heuristics to determine whether or not to inline a function. For example, the compiler takes into account the size of the function being inlined and the amount of inlining that has already been done in the current function. Therefore, seemingly insignificant changes in the source program can cause the warnings produced by
-Winline
to appear or disappear.
Check the generated code. If the function is expanded, you'll see its body, as opposed to a call
or similar instruction.
The compiler does not make a function inline if the function returns an address.
With gdb, if you cannot call to a function, one of its possible meanings is the function is inline. Flipping the reasoning, if you can call a function inside gdb, means the function is not marked inline.