Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Does It Mean For a C++ Function To Be Inline?

See title: what does it mean for a C++ function to be inline?

like image 869
user7545 Avatar asked Oct 01 '08 06:10

user7545


People also ask

How do you know if a function is inline?

The decision to inline or not a function is made by compiler. And since it is made by compiler, so YES, it can be made at compile time only. So, if you can see the assembly code by using -S option (with gcc -S produces assembly code), you can see whether your function has been inlined or not.

What is inline function and example?

Inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time.

When should you use inline in C?

Inline functions are commonly used when the function definitions are small, and the functions are called several times in a program. Using inline functions saves time to transfer the control of the program from the calling function to the definition of the called function.

Is there inline in C?

Inline Function are those function whose definitions are small and be substituted at the place where its function call is happened. Function substitution is totally compiler choice.


1 Answers

It means one thing and one thing only: that the compiler will elide multiple definitions of the function.

A function normally cannot be defined multiple times (i.e. if you place a non-inline function definition into a header and then #include it into multiple compilation units you will receive a linker error). Marking the function definition as "inline" suppresses this error (the linker ensures that the Right Thing happens).

IT DOES NOT MEAN ANYTHING MORE!

Most significantly, it does NOT mean that the compiler will embed the compiled function into each call site. Whether that occurs is entirely up to the whims of the compiler, and typically the inline modifier does little or nothing to change the compiler's mind. The compiler can--and does--inline functions that aren't marked inline, and it can make function calls to functions that are marked inline.

Eliding multiple definitions is the thing to remember.

like image 91
DrPizza Avatar answered Oct 04 '22 10:10

DrPizza