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 i开发者_C百科t is the responsibility of the programmer to decide how to define the function, based on run time and compilation-time considerations?
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.
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.
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.
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.
inline functions depends upon the length of the code and the complexity of the code...:) The inline function is just a request to the compiler...
精彩评论