inline redundancy?

This is a discussion on inline redundancy? within the C++ Programming forums, part of the General Programming Boards category; Hey fellas, I've been reading this tutorial and the guy uses the inline keyword like this: Code: class Foo { ...

  1. #1
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355

    inline redundancy?

    Hey fellas,

    I've been reading this tutorial and the guy uses the inline keyword like this:
    Code:
    class Foo
    {
    public:
         inline bool      Bar()       { return FooBar; }
    }
    Now I'm not sure (that's why I'm asking) but I think I read somewhere that this was unecessary, because a function like this will automatically be considered for inlining (at the compilers discretion).

    Is that correct?
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    As far as I know, any function defined inside of the class itself is automaticly inlined. I don't have the standard, so I can't tell you for sure. But my understanding of it is, if you define a function inside the class declaration itself, it's inlined.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Thanks that's what I thought. Of course there's no guarantee of inline right? It's a "request" and the compiler decides.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  4. #4
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Do you really need to define inline in the interface at all ? I thought it was enough to define it in the implementation. (I know it's all in one in this case, but imagine it was not, would I need to use inline in both, the interface and the implementation?)

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You need to have the implementation in the h file. So either put it in the class and it will be inlined ( hopefully ) or put it in the h file after the class with the inline keyword.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by HybridM
    Thanks that's what I thought. Of course there's no guarantee of inline right? It's a "request" and the compiler decides.
    Right. It's like the 'register' keyword. It tells the compler you'd like this to be in a register, but it doesn't force it there. (There has been huge arguments on this on boards in the past, but trust me it's the way it works. :P)

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    afaik you can force it to be inline somehow.

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by darksaidin
    afaik you can force it to be inline somehow.
    Not according to the C++ standard.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Nothing can fully force a function to always be inlined; usually, though, the compiler honors your request, or at the very least throws a warning when it can't.

  10. #10
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    I think some MS compilers have some sort of __force_inline.

    I'm not sure if it's MS or another company, or if the keyword I used was correct, but I've definately seen it before.

  11. #11
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Yes, MS has that, but even that doesn't make it 100% for certain inlined. It merely says to the compiler: "I don't care what optimizations you are doing, or whether you think it should be inlined or not, but *IF* the function can be inlined, do it."

    Some kinds of function calls can't be inlined (such as via a function pointer) so nothing can ALWAYS inline a function.

  12. #12
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    True, very true.

  13. #13
    Registered User kippen's Avatar
    Join Date
    Aug 2003
    Posts
    3
    just curious what is an inline function?

  14. #14
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    An inline function is a function that isn't called by the program like a regular function. Instead whenever the compiler sees a call to the inline function, it replaces the call with the actual code body of the function, thus avoiding the function call overhead.
    It's not always better and can sometimes be worse to inline rather than just call normally, though.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  15. #15
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    someblubb.h
    Code:
    class CBlubb {
      void Blubbba();
    }
    someblubb.cpp
    Code:
    inline void CBlubb::Blubbba() {
      Blob();
    }

    So that would inline Blubbba if possible and if not, give me a warning about it, right?

    btw, is it right that I can't have inlines in an abstract baseclass (of course not for abstract functions, but for functions that use abstract ones) ? If I inline those, the compiler always tells me it can't find an implmentation for *functionname*.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 51
    Last Post: 02-09-2009, 04:35 PM
  2. When to inline your *tors
    By Angus in forum C++ Programming
    Replies: 43
    Last Post: 10-29-2008, 03:38 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. inline??
    By bman1176 in forum C++ Programming
    Replies: 2
    Last Post: 06-25-2002, 05:31 PM

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