Should this function be a inline function?

This is a discussion on Should this function be a inline function? within the C++ Programming forums, part of the General Programming Boards category; I have a class for storing the position of an object in 2d, and I have to do some checking ...

  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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    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)

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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.

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

  10. #10
    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?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    So if you are using a good compiler, dont bother with inlining?
    It depends. Chances are at a high enough level of optimisation, the compiler will be inlining what needs to be inlined anyway. If not, as medievalelks is hinting, you could profile your code and suggest (or even force, with compiler extensions) that the function be inlined.

    That said, inlining is just one optimisation that can be applied, and if the function is not a good candidate for inlining, it would make things worse.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    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.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    So inlining is just for optimization?

    Its for a school assignment so I wanted to show off that I understood inline (at least I think I do)

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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,302
    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"

Page 1 of 2 12 LastLast
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, 01:46 PM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21