Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How will i know whether inline function is actually replaced at the place where it is called or not?

Tags:

c++

inline

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 at the place where it is called or not as decision of treating inline function as inline is at the compile time?

like image 813
Abhineet Avatar asked May 17 '12 07:05

Abhineet


People also ask

When the function code is replaced at that point that function is called as?

Inline function is a function that is expanded in line when it is invoked. That is, the compiler replaces the function call with the corresponding function code.

What happens when the keyword inline is placed in front of a function?

Answer. An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

When can an inline function not be used?

When we should avoid the use of inline? We should not use functions that are I/O bound as inline functions. When large code is used in some function, then we should avoid the inline. When recursion is used, inline function may not work properly.

What is inline function and how it can be called?

Advertisements. C++ inline function is powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.


1 Answers

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:


Analyze generated Assembly Code:

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.


Use Compiler specific Warnings and Diagnostics:

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.

like image 175
Alok Save Avatar answered Sep 22 '22 11:09

Alok Save