views:

245

answers:

4

In the problems of inline functions in wikipedia : http://en.wikipedia.org/wiki/Inline%5Fexpansion#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?

A: 

How do you prevent the GCC from inlining a C++ function?

See this question

Doug T.
A: 

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.

caspin
-1 In any reality the outcome of this code is not defined in the C++ spec. It is hard to see how your answer would help the asker gain clarity on the uses/misuse on the inline keyword.
Elemental
The question was two parts. Clarify the "language specification" comment and how to disable inlining in c++. The "language specificaiton" comment from wikipedia does not apply to c/c++ so I invented a language similar to C for an example. I in no way claim that what I wrote is valid c++ or even c. I tried to make that clear with the phrases "assume" and "in this strange world"
caspin
+4  A: 

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.

Jerry Coffin
+1  A: 

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.

Elemental