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. inline or macro

inline or macro

Scheduled Pinned Locked Moved C / C++ / MFC
cssquestion
16 Posts 11 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.
  • S Stuart Dootson

    Be programmer wrote:

    inline or macro

    There are very, very, very, very few reasons to ever consider using a macro rather than an inline function. In fact, your question is a subset of the second item in Scott Meyer's Effective C++ (one of the C++ textbooks you really want to read):

    Item 2: Prefer consts, enums, and inlines to #defines

    The reasons?

    1. Macros live outside the type system

    2. Macros pollute all namespaces

    3. There can be unexpected outcomes from macros. Consider the following macro function and it's use:

      #define CALL_WITH_MAX(a, b) f((a) > (b) ? (a) : (b))

      CALL_WITH_MAX(++a, b); // a is incremented twice
      CALL_WITH_MAX(++a, b+10); // a is incremented once

      Can you predict that reliably? Nope, thought not.

    So, to paraphrase...just don't ever use a macro instead of an inline function. Don't try to micro-optimise before you know what the slow things are...

    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

    K Offline
    K Offline
    KellyR
    wrote on last edited by
    #6

    In addition to what Stuart said, an inline function probably won't actually "inline" in the compiler if it's 20 lines long. If it's only a few lines, then it might. And I wouldn't really consider 20 lines to be that "small." Generally you only want to inline 1-liners or maybe a few more lines than that, but the compiler will ultimately decide what it lets you inline even if you specifically set the function to be an "inline" function.

    KR

    M 1 Reply Last reply
    0
    • D David Crow

      Michael Schubert wrote:

      Out of curiosity, what's a "Be programmer"?

      Whether to be or not to be, that's his question.

      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #7

      DavidCrow wrote:

      Michael Schubert wrote:

      Out of curiosity, what's a "Be programmer"?

      Whether to be or not to be, that's his question.

      ToBe OR NOT ToBe == -1

      :-\ Sorry, couldn't resist....

      "It's supposed to be hard, otherwise anybody could do it!" - selfquote
      "High speed never compensates for wrong direction!" - unknown

      1 Reply Last reply
      0
      • B Be programmer

        i am planning to write a function with less than 20 lines so i ma doubted whether to use inline or macro? which is better?

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #8

        Write a normal function/method. Anyway, the compiler will probably override the "inline" if the function is too complex. see http://www.parashift.com/c++-faq-lite/inline-functions.html[^]

        This signature was proudly tested on animals.

        C 1 Reply Last reply
        0
        • G George L Jackson

          He progressed from "A programmer"?

          "We make a living by what we get, we make a life by what we give." --Winston Churchill

          P Offline
          P Offline
          PJ Arends
          wrote on last edited by
          #9

          And soon he will only "see programmer"


          You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

          1 Reply Last reply
          0
          • K KellyR

            In addition to what Stuart said, an inline function probably won't actually "inline" in the compiler if it's 20 lines long. If it's only a few lines, then it might. And I wouldn't really consider 20 lines to be that "small." Generally you only want to inline 1-liners or maybe a few more lines than that, but the compiler will ultimately decide what it lets you inline even if you specifically set the function to be an "inline" function.

            KR

            M Offline
            M Offline
            Michael Schubert
            wrote on last edited by
            #10

            Regardless of the usefulness, you can force most compilers to inline (__forceinline), no matter how much code is involved.

            R 1 Reply Last reply
            0
            • M Michael Schubert

              Regardless of the usefulness, you can force most compilers to inline (__forceinline), no matter how much code is involved.

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #11

              Hi Mike, I always thought even with the __forceinline keyword, the compiler had an option to ignore it and not inline the function (if it feels so). So, is that wrong and the keyword causes the compiler to forcefully make the function inline?

              It is a crappy thing, but it's life -^ Carlo Pallini

              M C 2 Replies Last reply
              0
              • R Rajesh R Subramanian

                Hi Mike, I always thought even with the __forceinline keyword, the compiler had an option to ignore it and not inline the function (if it feels so). So, is that wrong and the keyword causes the compiler to forcefully make the function inline?

                It is a crappy thing, but it's life -^ Carlo Pallini

                M Offline
                M Offline
                Michael Schubert
                wrote on last edited by
                #12

                I had mixed results when I played around with this. The VC6 compiler does ignore __forceinline when a function is beyond a certain complexity/size, especially when there is a call to another function within. The Intel C++ compiler (at least newer versions) has a bunch of switches to control inlining and lets you inline almost every function. When you try that with a large function, the final .obj (and the resulting .exe) file(s) will be huge. After a lot of testing I realised that it's usually best to let the compiler decide (/Ob2). The Intel compiler is especially good in this regard.

                R 1 Reply Last reply
                0
                • M Maximilien

                  Write a normal function/method. Anyway, the compiler will probably override the "inline" if the function is too complex. see http://www.parashift.com/c++-faq-lite/inline-functions.html[^]

                  This signature was proudly tested on animals.

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #13

                  I second that. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  1 Reply Last reply
                  0
                  • R Rajesh R Subramanian

                    Hi Mike, I always thought even with the __forceinline keyword, the compiler had an option to ignore it and not inline the function (if it feels so). So, is that wrong and the keyword causes the compiler to forcefully make the function inline?

                    It is a crappy thing, but it's life -^ Carlo Pallini

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #14

                    Rajesh R Subramanian wrote:

                    I always thought even with the __forceinline keyword, the compiler had an option to ignore it and not inline the function (if it feels so).

                    You're right. Anyway, you may then consider __force_tha_fu@kin_function_inline_bastard directive, that usually do the job. :rolleyes:

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    R 1 Reply Last reply
                    0
                    • M Michael Schubert

                      I had mixed results when I played around with this. The VC6 compiler does ignore __forceinline when a function is beyond a certain complexity/size, especially when there is a call to another function within. The Intel C++ compiler (at least newer versions) has a bunch of switches to control inlining and lets you inline almost every function. When you try that with a large function, the final .obj (and the resulting .exe) file(s) will be huge. After a lot of testing I realised that it's usually best to let the compiler decide (/Ob2). The Intel compiler is especially good in this regard.

                      R Offline
                      R Offline
                      Rajesh R Subramanian
                      wrote on last edited by
                      #15

                      Michael Schubert wrote:

                      I had mixed results when I played around with this. The VC6 compiler does ignore __forceinline when a function is beyond a certain complexity/size, especially when there is a call to another function within.

                      Exactly! I've struggled with this a few years back (VC6 compiler) which ignored the keyword and made it a normal function, realising that the function is a little more complex than what should it be to qualify as an inline one. I wanted it to be inline because I kept some of the code for application validation and secure updates (based on the version purchased) in that. The function was being called from several places in the source and I didn't want it to be pointing to one function call (trying to make a cracker's job a little tough). So, I realised this is an arrogant display of - "I know it better than you, so you cannot force me" attitude by the MS compiler. They might as well remove this keyword; I don't find it to behave any different the __inline keyword. Good to know the intel compiler just does what we ask it to do. But, after all, MS knows what's best for us, better than we do. :)

                      It is a crappy thing, but it's life -^ Carlo Pallini

                      1 Reply Last reply
                      0
                      • C CPallini

                        Rajesh R Subramanian wrote:

                        I always thought even with the __forceinline keyword, the compiler had an option to ignore it and not inline the function (if it feels so).

                        You're right. Anyway, you may then consider __force_tha_fu@kin_function_inline_bastard directive, that usually do the job. :rolleyes:

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        R Offline
                        R Offline
                        Rajesh R Subramanian
                        wrote on last edited by
                        #16

                        I'd love to have the directive though. :-D

                        It is a crappy thing, but it's life -^ Carlo Pallini

                        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