开发者

Confusion with the problems of inline function

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 pro开发者_如何学Gocedures 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 inline keyword 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 inline keyword 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 inline keyword 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.


The inline keyword is a suggestion or prod to the compiler. The definition from wikipedia seems to imply that by use of a keyword of this sort you restrict the things you can do with the function. For example you might expect it not to be possible to take the address of an inline function. C++ compilers instead take the opposite tack, even if a function is marked inline, if somewhere in the code the address of said function is taken, then that function is not generated inline.

Similarly if a function is virtual is (obviously) cannot be generated inline but still this doesn't make inline definition of polymorphic functions illegal.

Perhaps what I wrote here will give you insight into what the compiler is required to do with the inline keyword as eloquently expressed by Coffin.


C/C++ is well specified when it comes to how functions can be inlined. So that particular comment from wikipedia does not apply to those languages.

Assume that the C language specification required that function call parameters be passed on the stack in reverse order (Edit: and that the stack always grown downward, and that no padding be placed between arguments). In reality they usually are but you are not allowed to assume that is always true. The following code would be valid in this strange world.

void foo( int i, int j )
{
  int myi = &j[1];
  return myi + j;
}

if foo where inlined the integer that occurs on the stack above j may not be i.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜