Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How will this code compile

Suppose we have below code:

inline void DoSome()
{
    cout << "do some" << endl;
}

int main()
{
    void (*pDoSome)() = DoSome;

    DoSome(); // one
    pDoSome(); // two
}

For above code we have three possible scenarios:

  1. one will be inlined, two won't
  2. one and two will be inlined
  3. one and two won't be inlined (because we took the address of function)

Now I want know which of the above scenarios is true?

like image 523
alireza sadeghpour Avatar asked May 18 '26 06:05

alireza sadeghpour


2 Answers

inline is a hint to the compiler, but it is not an obligation. It is up to the compiler to decide if a function declared as inline will actually be inlined and thus any of the two calls may or may not be inlined.

like image 178
Ivaylo Strandjev Avatar answered May 20 '26 20:05

Ivaylo Strandjev


All scenarios are possible, because the compiler may optimise as it wants. The only option to see what happened is to look in the assembler output.

like image 39
Wojtek Surowka Avatar answered May 20 '26 19:05

Wojtek Surowka