Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. what is the difference between "static" function and "inline" function? [SOLVED]

what is the difference between "static" function and "inline" function? [SOLVED]

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++debugging
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yu jian
    wrote on last edited by
    #1

    I use the VC++ 2008 to check this question. I define one function named "max2" using keyword "static" and one function named "min2" using keyword "inline". I think that the compiler will use the code inside the min2 function to replace the function called. And the max2 with static keyword will be compiled to be a function. But after the Deassembled in Debug model. I found that the exe both use the "call " assemble code to call these two functions. why does the inline not succeed?

    S P 2 Replies Last reply
    0
    • Y yu jian

      I use the VC++ 2008 to check this question. I define one function named "max2" using keyword "static" and one function named "min2" using keyword "inline". I think that the compiler will use the code inside the min2 function to replace the function called. And the max2 with static keyword will be compiled to be a function. But after the Deassembled in Debug model. I found that the exe both use the "call " assemble code to call these two functions. why does the inline not succeed?

      S Offline
      S Offline
      SoMad
      wrote on last edited by
      #2

      You have the idea right, but there are several issues with inline. Here is a list off the top of my head: 1. You can change the setting in the Project Properties -> C/C++ -> Optimizations -> Inline Function Expansion. 2. You say you de-assembled the Debug build of your exe, but for Debug builds the default setting is Disabled. 3. The inline keyword does not order the compiler to expand the function inline, it is merely a request. The compiler will determine if it is worth making it inline or if it is better left as a function call. 4. If a function is marked as inline and called from multiple other function, it might be inlined in some of the calling functions and not in others. Again the compiler determines whether it is worth it. 5. If you discover that the compiler is not inlining a function you have marked as inline and you really, really, really want it inlined because you think you know better or you want to run profiling with it inlined and not inlined, try using the __forceinline keyword. http://msdn.microsoft.com/en-us/library/z8y1yy88(v=vs.90).aspx[^] Soren Madsen

      "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

      Y 1 Reply Last reply
      0
      • Y yu jian

        I use the VC++ 2008 to check this question. I define one function named "max2" using keyword "static" and one function named "min2" using keyword "inline". I think that the compiler will use the code inside the min2 function to replace the function called. And the max2 with static keyword will be compiled to be a function. But after the Deassembled in Debug model. I found that the exe both use the "call " assemble code to call these two functions. why does the inline not succeed?

        P Offline
        P Offline
        pasztorpisti
        wrote on last edited by
        #3

        A static method can be inlined if the compiler decides to do so, and an inline function might be instantiated as a callable function. Its all up to the compiler. In general it can easily happen that a compiler creates both an inlined and instantiated version of the very same function (no matter if its static or inlined).

        1 Reply Last reply
        0
        • S SoMad

          You have the idea right, but there are several issues with inline. Here is a list off the top of my head: 1. You can change the setting in the Project Properties -> C/C++ -> Optimizations -> Inline Function Expansion. 2. You say you de-assembled the Debug build of your exe, but for Debug builds the default setting is Disabled. 3. The inline keyword does not order the compiler to expand the function inline, it is merely a request. The compiler will determine if it is worth making it inline or if it is better left as a function call. 4. If a function is marked as inline and called from multiple other function, it might be inlined in some of the calling functions and not in others. Again the compiler determines whether it is worth it. 5. If you discover that the compiler is not inlining a function you have marked as inline and you really, really, really want it inlined because you think you know better or you want to run profiling with it inlined and not inlined, try using the __forceinline keyword. http://msdn.microsoft.com/en-us/library/z8y1yy88(v=vs.90).aspx[^] Soren Madsen

          "When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

          Y Offline
          Y Offline
          yu jian
          wrote on last edited by
          #4

          Thank you for you reply.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups