tags:

views:

98

answers:

5

hi,

what are the requirement for a function so it could be executed inline in c++? is there a case that a function can't be inline? or any function can be inline and it is the responsibility of the programmer to decide how to define the function, based on run time and compilation-time considerations?

A: 

Inline functions are functions where the call is made to inline functions. The actual code then gets placed in the calling program.

Normally, a function call transfers the control from the calling program to the function and after the execution of the program returns the control back to the calling program after the function call. These concepts of function saved program space and memory space are used because the function is stored only in one place and is only executed when it is called. This concept of function execution may be time consuming since the registers and other processes must be saved before the function gets called.

The extra time needed and the process of saving is valid for larger functions. If the function is short, the programmer may wish to place the code of the function in the calling program in order for it to be executed. This type of function is best handled by the inline function. In this situation, the programmer may be wondering “why not write the short code repeatedly inside the program wherever needed instead of going for inline function?” Although this could accomplish the task, the problem lies in the loss of clarity of the program. If the programmer repeats the same code many times, there will be a loss of clarity in the program. The alternative approach is to allow inline functions to achieve the same purpose, with the concept of functions.

here is the benefits discussed
http://stackoverflow.com/questions/145838/benefits-of-inline-functions-in-c

http://www.cplusplus.com/forum/articles/20600/

org.life.java
+3  A: 

It depends what you mean. If you mean, when can the function be expanded in-line, removing a function call, then that is really up to the compiler. It may inline almost any function, and refuse to inline almost any function that you ask it to. Functions that probably won't be inlined include recursive ones, and ones where you have taken the function's address. On the whole, it's better not to think about this much.

The main use for the inline keyword is not to request inlining, but to indicate that the function may be #included in multiple translation units without causing multiple definition errors.

anon
+1. But many recursive functions *can* actually be inlined (in particular, but not limited to, tail recursive functions).
Konrad Rudolph
A: 

or any function can be inline and it is the responsibility of the programmer to decide how to define the function, based on run time and compilation-time considerations?

It's the compiler that basing on some conditions uses the function as inline function.

kiamlaluno
+2  A: 

what are the requirement for a function so it could be executed inline in c++? is there a case that a function can't be inline?

An inline function is just one where the function call is replaced with the actual code. If you go by that definition, you could copy and paste the code manually and it would be "inline". However, that may not always be a good idea: it's a matter of speed vs. program size. As functions grow larger, the benefit of having them inline reduces.

or any function can be inline and it is the responsibility of the programmer to decide how to define the function, based on run time and compilation-time considerations?

Normally, when you use the inline keyword, it is only a request and the compiler chooses whether or not to actually make the function call inline. Many compilers also provide a way to force a function to be inline (for eg. MSVC has the __forceinline keyword) - in this case, the compiler doesn't try to be smart, so it's up to you to weigh the benefits of making the function inline.

casablanca
Even `__forceinline` won’t work in all cases – consider recursive functions that can’t be refactored to be tail recursive. In such cases, only the outer call to the function can be inlined, but not the subsequent calls (to itself).
Konrad Rudolph
@Konrad: True, I didn't think about that.
casablanca
Yes, __forceinline does not actually force the compiler to inline.
DeadMG
@DeadMG: Well, there are certain cases where it's not *possible* to inline (Konrad pointed out one such case, more [in the docs](http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx)) but in all other cases `__forceinline` overrides the compiler's "choice".
casablanca
+1  A: 

what are the requirement for a function so it could be executed inline in c++?

It needs to be defined at every spot in which it's called (typically this is done by putting it in a .h).

is there a case that a function can't be inline?

Not in terms of the language standard, I believe, although of course each compiler can and will implement some restrictions of its own.

or any function can be inline and it is the responsibility of the programmer to decide how to define the function, based on run time and compilation-time considerations?

The inline keyword is just a hint from the programmer to the compiler that the programmer would really like this function to be inlined (presumably the programmer has spotted substantial call overhead with a small function being called in "hot" loops as shown by the profiler -- or, the function is so tiny that it's about as small as the calling code;-) -- or, inlining the function allows "optimization across function boundaries" that a particular compiler can't spot or can't perform otherwise -- and so forth).

The compiler is free to ignore the hint just as it's free to ignore the older register hint for a variable's storage class (I believe nowadays most optimizing C++ compilers do ignore register but fewer ignore inline) -- IOW, the compiler is free to inline all or some of the calls to a function whether that function is declared inline or not.

(Of course it won't "inline calls" when they're done through an explicit pointer to the function that is stashed away at some points and used at others, or when the function's address is passed as a parameter to some other function - but that might affect the inlining of specific calls, not necessarily other calls to the same function that are done differently).

"Just in case" your compiler takes your inline hints very earnestly, it's often worth measuring code size and speed with and without inline around (unless your compiler offers an option/flag for the purpose, just a #define inline will definitely "disable" the effect of inline and thereby allow such measurement). But if you're going to deploy your code across multiple compilers, esp. for multiple architectures, be aware that the effects that are positive on one platform may end up being counterproductive on another, given the difference in compilers' optimizaiton strategies and in CPU architectures.

Alex Martelli