0

I was searching the internet a few topics related to inline functions, but none of them took my doubts.

So far I know the inline function works the same way that a method or a block, but recently saw the following response:

By declaring a function inline you tell the compiler to replace the complete code of that function directly into the place from where it was called. This is a rather advanced feature that requires understanding of lower-level programming.

So that means that in normal functions: I was at a point A and wanted to go to point B for that I get out of A and go to B, right?

Have inline functions works in this way: I was at point A and would like to go to point B for that point B comes up to point A?

What I said above is correct?

jscs
  • 63,694
  • 13
  • 151
  • 195
LettersBa
  • 747
  • 1
  • 8
  • 27
  • 2
    By declaring a function inline, you exclude it from the One Definition Rule. That's about all that's guaranteed. – Benjamin Lindley Jan 11 '15 at 19:29
  • Basically, it's not even a function, it's like you'd declare a macro such as #define max(a,b). Just safer :D – Zach P Jan 11 '15 at 19:34
  • @BenjaminLindley Hmm. It's the first time this news reached me in this direct wording. Do you have an authoritative source for this? Would even be worth having on SO if it isn't yet – sehe Jan 11 '15 at 19:39
  • 1
    @BenjaminLindley "exclude" isn't quite the right word. It can be defined multiple times, but only if each definition is in a different translation unit, and the definitions must be exactly the same. – T.C. Jan 11 '15 at 19:41
  • @user3195614: Inline functions **are** functions. They are not equivalent to macros. The `inline` keyword is a suggestion to the compiler on how to process a function. The keyword does not change the functionality of the function. – Thomas Matthews Jan 11 '15 at 19:41
  • @sehe I'd say it follows from C++11 7.1.2 (esp. §2) and 3.2/5 – Angew is no longer proud of SO Jan 11 '15 at 19:44
  • @Angew only by reversing it could imply lots more than actually intended. T.C. already confirmed and my reading of §7.1.2 matches ("each definition of D shall consist of the same sequence of tokens"). So there's really /no/ exclusion of the ODR whatsoever - which is about this equivalence – sehe Jan 11 '15 at 19:45
  • Also, which language are we talking about here? C++ `inline` and C99 `inline` are different beasts. – T.C. Jan 11 '15 at 19:49
  • @ThomasMatthews I know that, I just try to compare it with C for his understanding... – Zach P Jan 11 '15 at 20:01

3 Answers3

2

Ideally, with the inline keyword, the compiler pastes the contents of the inlined function at the point where the function is called.

Given:

void Print(void)
{
  cout << "Hello World!\n";
}

int main(void)
{
  Print();
  return 0;
}

The compiler would emit an assembly instruction to call the Print function inside the main function.

When declaring the Print function as inline, the compiler would generate the conceptual main() function below:

int main(void)
{
  // Substitute content of Print function because it's declared as inline
  cout << "Hello World!\n";

  return 0;
}

Remember that the inline keyword is a suggestion to the compiler and the compiler can ignore it. The compiler may already inline small functions without using the inline keyword.

Long ago, the inline keyword was used to force compilers to paste code where the function was invoked. This technique was used to eliminate function call overhead. This was in the times when compilers were less intelligent about optimizing.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
1

The quote should read "replace the call" ... "place the complete code". The ideal behind an inline function is similar to a macro; in every place in the source code that calls the inline function, instead of calling the inline function, the call is replaced with the complete code from the inline function, and no call is made. This could generate a lot of duplicate code, but should result in faster execution, unless the increase in code size causes issues with cache. In some cases, a compiler may ignore the inline option and use a normal call.

rcgldr
  • 27,407
  • 3
  • 36
  • 61
0

Inline functions is a good tool when you want to save running time because when you call this kind of a function(inline) its code is then written in the call's place. Why does it save time? Because calling a regular function simply consumes more time than just pasting its functionality. Example:

inline void sayHi()
{ 
    std::cout<<"Hi!";
}
int main()
{
    sayHi();         //Calling an inline function 
    std::cin.get();
}

compiler pastes std::cout<<"Hi!"; instead of sayHi(); So what you said is actually correct.

zilvinas
  • 106
  • 1
  • 4