In the problems of inline functions in wikipedia :
http://en.wikipedia.org/wiki/Inline_expansion#Problems
it says : “# A language specification may allow a program to make additional assumptions about arguments to procedures that it can no longer make after the procedure is inlined.”
Could somebody elaborate this point?
How do you prevent the GCC from inlining a C++ function?
In C++, the
inlinekeyword really only has one required meaning: that the One-Definition Rule is suspended for that function (e.g., the function can be defined in several translation units, and the code still conforms).Specifically, using the
inlinekeyword does not ensure that the code for that function will be generated inline. Defining a function inside a class definition also makes it an inline function — but, again, that doesn’t ensure that its code will be generated inline either.Conversely, a function that is defined outside a class definition, without the
inlinekeyword can and may still have its code generated inline. The only difference is that in this case multiple definitions of the function renders the code non-conforming.The bottom line is that portable code cannot assure that code either is or is not generated inline. If you don’t mind making your code non-portable, however, you can use
__attribute__(noinline).I would not, however, do this on the basis of the cited quote from Wikipedia. Wikipedia is hardly an authoritative source, and even if it was, what you’re quoting is just a vague statement about what could happen with some hypothetical language on some hypothetical compiler under some hypothetical conditions. You’re generally better off writing your code to be clear and readable, and letting the compiler worry about generating good results from that.