Thread: Should this function be a inline function?

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by h3ro View Post
    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)
    Yes, that's what it's for. Instead of making the function call each time, the compiler expands it "inline" in the code where it was called. As you might expect, too much inlining can bloat your code.

    It's good that you are inquisitive and want to learn new things. I wasn't trying to bust your chops.

    If you *really* want to impress your teacher, though, download UnitTest++ and verify that your code works with automated unit tests. Or code up your own test harness to drive your code and validate it.

  2. #17
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by iMalc View Post
    Don't prematurely inline.

    If you don't know that you need to, then you shouldn't do it.
    There are other ways of "knowing" than resorting to a profiler. Experience is a valid way of knowing.

  3. #18
    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.

  4. #19
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by brewbuck View Post
    There are other ways of "knowing" than resorting to a profiler. Experience is a valid way of knowing.
    Either way, it's still premature if you do it without performance mandating it.

  5. #20
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by medievalelks View Post
    Either way, it's still premature if you do it without performance mandating it.
    That would be the case if it were somehow easier to not declare it inline than otherwise. But it requires no measurable additional effort to declare a function inline. In fact, for very simple classes, declaring every function in a class inline requires less effort, because you then don't need to create a source file at all.
    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.

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by King Mir View Post
    That would be the case if it were somehow easier to not declare it inline than otherwise. But it requires no measurable additional effort to declare a function inline. In fact, for very simple classes, declaring every function in a class inline requires less effort, because you then don't need to create a source file at all.
    I didn't say it was bad in that case, just that optimization without measurement (formal or informal) is by definition premature.

  7. #22
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by medievalelks View Post
    Either way, it's still premature if you do it without performance mandating it.
    Don't misunderstand the principle. The purpose of the premature optimization rule of thumb is to prevent the growth of overly intricate, difficult-to-understand code. (EDIT: It's not like there is some principle which says, "Write the slowest code possible until proven otherwise") However, the inline optimization is categorically different. Does the addition of the keyword "inline" suddenly cause a function to become hard to understand?

    And the argument that it produces code bloat is flawed, because a non-stupid compiler will not inline functions when it would lead to code bloat in the first place, regardless of whether you've hinted at it with "inline."

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I didn't say it was bad in that case, just that optimization without measurement (formal or informal) is by definition premature.
    Experience could count as an informal measurement. I guess that this could be related to preferring ++i over i++, or passing objects of class types by (const) reference instead of by value by default.

    I think that in this case one has the compiler's judgment as a safety net, so even if one's experience proves inapplicable, the compiler may nudge things along the right path 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

  9. #24
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    Does specifying a function as inline make it more likely that the compiler will actually inline it?
    not necessarily. if the function is completely contained in the only source file that it's called from I've noticed vs2005 will attempt to inline it, regardless of whether it's declared using the inline specifier.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  10. #25
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Don't misunderstand the principle. The purpose of the premature optimization rule of thumb is to prevent the growth of overly intricate, difficult-to-understand code. (EDIT: It's not like there is some principle which says, "Write the slowest code possible until proven otherwise") However, the inline optimization is categorically different. Does the addition of the keyword "inline" suddenly cause a function to become hard to understand?
    Quote Originally Posted by laserlight View Post
    Experience could count as an informal measurement. I guess that this could be related to preferring ++i over i++, or passing objects of class types by (const) reference instead of by value by default.
    Uncle :-)

  11. #26
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by medievalelks View Post
    I didn't say it was bad in that case, just that optimization without measurement (formal or informal) is by definition premature.
    What I'm saying is that it doesn't count as optimization, because there in no extra effort involved in implementing it.

    Even if an application is not speed critical, it is still perfectly fine to inline code, provided that no special effort is devoted to doing so.
    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.

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

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