Search code examples
c++function-pointersinline-functions

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?


Solution

  • 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.