title: inline expansion compiles into a function call [c++]
body: <h2>Introduction:</h2>
  
  <p>I have been creating a simple wrapper classes. I randomly found out that <em>(or it appears to be)</em> an inline function still compiled into a function call. I created an example class to test things out and this is what I found:</p>
  
  <p><strong>Consider the following class:</strong> </p>
  
  <pre><code>//compile with MSVC
  class InlineTestClass
  {
  public:
      int InternalInt;
  
      int         GetInt()        {return InternalInt;}
      inline int  GetInt_Inl()    {return InternalInt;}                
      //__forceinline -Forces the compiler to implement the function as inline
      __forceinline int  GetInt_ForceInl()  {return InternalInt;} 
  };
  </code></pre>
  
  <p><strong>This class has 3 functions for reference.</strong> </p>
  
  <ul>
  <li>The <strong>GetInt</strong> function is a standard function. </li>
  <li>The <strong>GetInt_Inl</strong> function is an inline function.</li>
  <li>The <strong>GetInt_ForceInl</strong> function is an ensured inline function in case of the 
  compiler deciding not to implement <strong>GetInt_Inl</strong> as inline function</li>
  </ul>
  
  <p>Implemented like so:</p>
  
  <pre><code>InlineTestClass itc;
  itc.InternalInt = 3;
  int myInt;
  
  myInt = itc.InternalInt;       //No function
  myInt = itc.GetInt();          //Normal function
  myInt = itc.GetInt_Inl();      //Inline function
  myInt = itc.GetInt_ForceInl(); //Forced inline function
  </code></pre>
  
  <p>The resulting assembler code of the setting of myInt (taken from dissassembler):</p>
  
  <pre><code>       451        myInt = itc.InternalInt;
  0x7ff6fe0d4cae  &lt;+0x003e&gt;         mov     eax,dword ptr [rsp+20h]
  0x7ff6fe0d4cb2  &lt;+0x0042&gt;         mov     dword ptr [rsp+38h],eax
          452        myInt = itc.GetInt();
  0x7ff6fe0d4cb6  &lt;+0x0046&gt;         lea     rcx,[rsp+20h]
  0x7ff6fe0d4cbb  &lt;+0x004b&gt;         call    nD_Render!ILT+2125(?GetIntInlineTestClassQEAAHXZ) (00007ff6`fe0d1852)
  0x7ff6fe0d4cc0  &lt;+0x0050&gt;         mov     dword ptr [rsp+38h],eax
          453        myInt = itc.GetInt_Inl();
  0x7ff6fe0d4cc4  &lt;+0x0054&gt;         lea     rcx,[rsp+20h]
  0x7ff6fe0d4cc9  &lt;+0x0059&gt;         call    nD_Render!ILT+1885(?GetInt_InlInlineTestClassQEAAHXZ) (00007ff6`fe0d1762)
  0x7ff6fe0d4cce  &lt;+0x005e&gt;         mov     dword ptr [rsp+38h],eax
          454        myInt = itc.GetInt_ForceInl();
  0x7ff6fe0d4cd2  &lt;+0x0062&gt;         lea     rcx,[rsp+20h]
  0x7ff6fe0d4cd7  &lt;+0x0067&gt;         call    nD_Render!ILT+715(?GetInt_ForceInlInlineTestClassQEAAHXZ) (00007ff6`fe0d12d0)
  0x7ff6fe0d4cdc  &lt;+0x006c&gt;         mov     dword ptr [rsp+38h],eax
  </code></pre>
  
  <p>As shown above the setting (of myInt) from the <strong>member</strong> of InlineTestClass directly is <em>(as expected)</em> 2 mov instructions long. 
  Setting from the <strong>GetInt</strong> function results in a function call <em>(as expected)</em>, however both of the  <strong>GetInt_Inl</strong> and <strong>GetInt_ForceInl</strong> (inline functions) also result in a function call. </p>
  
  <p>It appears as if the inline function has been compiled as a normal function ignoring the inlining completely <em>(correct me if I am wrong)</em>.</p>
  
  <p>This is strange cause according to <a href="https://msdn.microsoft.com/en-us/library/bw1hbe6y.aspx" rel="nofollow noreferrer">MSVC documentation</a>:</p>
  
  <blockquote>
    <p>The inline and __inline specifiers instruct the compiler to insert a
    copy of the function body into each place the function is called.</p>
  </blockquote>
  
  <p>Which <em>(I think)</em> would result in:</p>
  
  <pre><code>inline int  GetInt_Inl() {return InternalInt; //Is the function body}
  myInt = itc.GetInt_Inl(); //Call site 
  
  //Should result in
  myInt = itc.InternalInt; //Identical to setting from the member directly
  </code></pre>
  
  <p>Which means that the assembler code should be also identical to the one of setting from the class member directly but it isn't.</p>
  
  <h2>Questions:</h2>
  
  <ol>
  <li>Am I missing something or implementing the functions incorrectly?</li>
  <li>Am I interpreting the function of the inline keyword? What is it?</li>
  <li>Why do these inline functions result in a function call?</li>
  </ol>