Thread: Should this function be a inline function?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Should this function be a inline function?

    I have a class for storing the position of an object in 2d, and I have to do some checking before a value is assign to the X and Y.

    Should the "checkRange()" function be inline?

    Code:
    class Position
    {
       public:
          Position(int nx, int ny);
          void setPos(int nx, int ny);
    
       private:
          int x,y;
    };
    
    void Point::setPos(int nx, int ny)
    {
       x = nx;
       checkRange(0,50,nx);
    
       y = ny;
       checkRange(0,100,ny);
    }
    
    void checkRange(int min, int max, int &value)
    {
       if(value > max)
          value = max;
    
       if(value < min)
          value = min;
    }

  2. #2
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    The compiler will decide on its own if it should be inlined or not. Typically, when working with modern compilers, they automatically inline functions if it's advantageous.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I've always wondered why the inline keyword exists, since compilers can inline without it and they can refuse to inline even if you specify inline?
    Does specifying a function as inline make it more likely that the compiler will actually inline it?

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    So does that mean the compiler will inline if and only if you suggest it as a inlinable candidate?

    Example:
    Code:
    inline void someObj::someFunc(){ // May be inlined if compiler agrees to it?
    
    return;
    }
    
    void someObj::anotherFunc(){ // Will never be inlined because of no explicit declaration?
    
    return;
    }
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dudeomanodude View Post
    So does that mean the compiler will inline if and only if you suggest it as a inlinable candidate?
    No, because "if and only if" is a total implication, and we've already established that it is free to not inline a function even when it is declared inline.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    I've always wondered why the inline keyword exists, since compilers can inline without it and they can refuse to inline even if you specify inline?
    Does specifying a function as inline make it more likely that the compiler will actually inline it?
    Declaring a function "inline" causes the linker to ignore multiple definition errors. That's pretty dang important.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by cpjust View Post
    I've always wondered why the inline keyword exists, since compilers can inline without it and they can refuse to inline even if you specify inline?
    Does specifying a function as inline make it more likely that the compiler will actually inline it?
    Compilers are/were generally designed to compile each source file separately. With this practice, it is impossible for a compiler to inline a function that written in a different source file. The solution: have potential inline candidates be included in header files. The problem with that is multiple definition errors. So the inline key word was invented to allow multiple definitions for functions intended to be inlined.

    People say that gcc is smart about figuring out which functions to inline, as far as I can tell, it cannot possibly inline a function unless a) it is declared inline, and included in a header, or b) it is declared in the source file unit that it is used in. Basically it boils down to a gcc can't inline a function call if the function being called is not included in the same compilation unit.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by King Mir View Post
    Compilers are/were generally designed to compile each source file separately. With this practice, it is impossible for a compiler to inline a function that written in a different source file. The solution: have potential inline candidates be included in header files. The problem with that is multiple definition errors. So the inline key word was invented to allow multiple definitions for functions intended to be inlined.

    People say that gcc is smart about figuring out which functions to inline, as far as I can tell, it cannot possibly inline a function unless a) it is declared inline, and included in a header, or b) it is declared in the source file unit that it is used in. Basically it boils down to a gcc can't inline a function call if the function being called is not included in the same compilation unit.
    There are, of course, further optimizations. I don't know if it applies to inline, but the linker can also perform optimizations on code after the compiler. These optimizations are at least available in Visual Studio.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The compiler will decide on its own if it should be inlined or not. Typically, when working with modern compilers, they automatically inline functions if it's advantageous.
    You might need to specify a high enough level of optimisation, or specify the particular optimisation that causes good candidates to be automatically inlined, or use the inline keyword to suggest suitable candidates to the compiler.

    In this case it probably is a suitable candidate to be inlined, but if the compiler disagrees, it will not inline it anyway.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So does that mean the compiler will inline if and only if you suggest it as a inlinable candidate?
    No. For example, g++ with the -O3 option or -finline-functions option will inline functions that it considers good candidates for inlining, even if they have not been declared as inline. If they have been declared as inline, then they might be inlined at a lower level of optimisation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    When you profile your code, does this function appear to be a bottleneck?

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    When you profile your code, does this function appear to be a bottleneck?
    Not at all, but thought that that function should be inline so I wanted to ask. I never used the inline keyword before.

    So if you are using a good compiler, dont bother with inlining?

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by h3ro View Post
    Not at all, but thought that that function should be inline so I wanted to ask. I never used the inline keyword before.

    So if you are using a good compiler, dont bother with inlining?
    Don't bother with premature optimization. If your code is slow, profile it. Then fix what is slowing it down.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by medievalelks View Post
    Don't bother with premature optimization. If your code is slow, profile it. Then fix what is slowing it down.
    A profiler is a bad way to find inlining candidates. Inlined code can trigger a cascade of optimizations which completely change the profile, not just a few particular timings.

    Some functions, frankly, are braindead obvious for inlining. The one originally mentioned by the OP is a borderline case. I would not personally inline it, at least initially, but I wouldn't view it as a premature optimization, either.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't prematurely inline.

    If you don't know that you need to, then you shouldn't do it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM