Thread: inheritance and operator<<

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The committee is working on revisions to the C++ standard, but don't expect it this week or anything. 2003 is the most recent.

    A function outside a class declaration is not inlined automatically, if you mean that every such function is inlined. Assuming you've turned on optimizations (I think, anyway; maybe even without), most compilers will consider such functions for inlining whether you say anything about it or not automatically.

    EDIT: Assuming the compiler can see the function definition, as mentioned above.

  2. #17
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    you should put it in a header file and use the inline key word.
    Header file or not, that doesn't matter? even if you put it in the implementation file (.cpp) , the inline keyword will request the compiler to inline it?

    ps: A function implementation in the class declaration will always be inline as far as I know.

    EDIT: I am speaking of class-member functions. I agree free functions should be put in a header file, but doing otherwise will make them useless?

  3. #18
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    A function implementation in the class declaration will always be inline as far as I know.
    O_o

    It may be treated as if it was marked with 'inline', but implementing a function inside the class declaration doesn't guarantee anything about the source being inlined.

    Soma

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MarkZWEERS View Post
    Header file or not, that doesn't matter? even if you put it in the implementation file (.cpp) , the inline keyword will request the compiler to inline it?
    If the function is in another source file and you want the possibility to inline it, then it should be put inside the header with the inline keyword. Otherwise you don't really need to mark the function with inline at all, because the compiler will inline it if it thinks it's a good idea.

    EDIT: I am speaking of class-member functions. I agree free functions should be put in a header file, but doing otherwise will make them useless?
    All functions should go into source files, unless you may think them suitable for inlining, in which case you probably should put them inside headers.
    This isn't required with templates, of course.
    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.

  5. #20
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by MarkZWEERS View Post
    Header file or not, that doesn't matter? even if you put it in the implementation file (.cpp) , the inline keyword will request the compiler to inline it?

    EDIT: I am speaking of class-member functions. I agree free functions should be put in a header file, but doing otherwise will make them useless?
    If a function is not included in every compilation unit that uses it, then most compilers cannot make it inline; the definition must be visible for the compiler to inline a function.

    I'm not sure if a function will link properly if it is defined in another compilation unit as being inline (and not defined in the compilation unit that it is used in). Regardless, excepting static functions and private methods, functions that you want the compiler to consider for inlining should be put in the header file. Static functions and private methods are an exception because they can only be used in one compilation unit, assuming common C++ conventions are followed.

    To reiterate:
    The sole use of the inline key word is used to allow a function definition to be included in multiple compilation units. Including the function definition in the compilation unit that uses it is required to inline a function.

    ps: A function implementation in the class declaration will always be inline as far as I know.
    It is treated the same way as a method defined with the inline keyword. It is up to the compiler to choose to inline it.
    Last edited by King Mir; 06-10-2008 at 02:04 PM.
    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
    May 2008
    Location
    Paris
    Posts
    248
    Huh? But then what does this do?
    This is a request to the compiler for inlining?

    .h file:
    Code:
    #ifndef blabla
    #define blabla
    
    class TestClass
    {
      __  function();
    };
    #endif
    the corresponding .cpp file :
    Code:
    #include "blabla.h"
    
    inline
    __  TestClass :: function() 
    {
      .....
    }
    And this forces the compiler to inline it?

    .h file:
    Code:
    #ifndef blabla
    #define blabla
    
    class TestClass
    {
      __  function()
        {
          ....
        }
    };
    #endif
    .cpp file (just to compile)
    Code:
    #include "blabla.h"

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    They're both the same. There is no forcing the compiler to inline a function if it doesn't want to.

  8. #23
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    The standard says:

    Code:
    7.1.2 Function specifiers
    
    3   A function defined within a class definition is an inline function. The inline specifier shall not appear
         on a block scope function declaration.\footnote{The inline keyword has no effect on the linkage of a function.}
    ps: the code tags are necessary to parse the \footnote ... can't we make a Latex parser? ;-)

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    We repeat:
    You cannot force the compiler to inline anything. It will do so at its own discretion.
    You can tell the compiler, however, or suggest to it, that you want a function to be inlined. This is done with the inline keyword.
    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.

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And?

    The inline keyword, as mentioned above, just allows multiple definitions of the same function (since you're including it in a header, which gets included who knows how many times), etc. The standard does not specify anything about the actual machine code generated. Putting it in the class definition makes a function an inline function. Marking it as such makes a function an inline function just the same. What happens in the actual executable is up to the compiler.

    And yes, we should have a LaTeX parser. I'm sure you'll get right on working on it.

  11. #26
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by tabstop View Post
    They're both the same. There is no forcing the compiler to inline a function if it doesn't want to.
    No standard way, at least. Most compilers provide switches that allow you to force inlining, or tune the inlining heuristics. But that's not something you achieve at the source code level.

  12. #27
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    What happens in the actual executable is up to the compiler.
    OK, I guess I had missed that one. On what does it depend, purely compiler and version? Do you know any differences between the g++ and microsoft's C++ compiler?

    I'm sure you'll get right on working on it.
    please notice my kind-of-smiley ;-) ;-)

    But I know some nice solutions exist to parse latex scripts, I will hunt for them.

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I know that Microsoft compiler has a switch to say "only inline functions marked for inlining" or "inline anything that the compiler thinks makes sense". gcc (g++) has a different set of options. With a recent (3.4 -> 4.x) will, given a -O3 option, inline functions called only once, no matter how large they are (at least functions hundreds of bytes long will be inlined - there's possibly a limit, but on the other hand, if the function is only called once [e.g. static functions, so the compiler knows that there's no external call to the function] then there's no benefit from making it a separate function - except when you want to debug the assembler code and need to know where the actual code came from, that is...)

    I have a feeling gcc is slightly more willing to inline functions than MS compiler - but that's just a feeling, and I have not spent any time attempting to determine which does the best job.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MarkZWEERS View Post
    OK, I guess I had missed that one. On what does it depend, purely compiler and version? Do you know any differences between the g++ and microsoft's C++ compiler?
    Yes, compiler specific.
    And Microsoft's compiler does not allow you to force it inline functions, but you can give it a strong suggestion to do so.
    It also allows you to tell the compiler to inline any suitable functions it finds.
    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.

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Yes, compiler specific.
    And Microsoft's compiler does not allow you to force it inline functions, but you can give it a strong suggestion to do so.
    It also allows you to tell the compiler to inline any suitable functions it finds.
    Have they removed the __forceinline keyword then? It used to be available in MS compilers - but of course, it prevents the code from being compiled with other compilers unless you start introducing macros and stuff.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator<< overload problem
    By stewie griffin in forum C++ Programming
    Replies: 5
    Last Post: 03-10-2009, 06:29 PM
  2. define operator<< for std::pair<string, int>
    By patiobarbecue in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2009, 08:29 AM
  3. ostream operator<< () overload problems
    By jaybny in forum C++ Programming
    Replies: 2
    Last Post: 06-24-2006, 11:00 PM
  4. no match for 'operator<<' in.... Compile Problem!
    By njd in forum C++ Programming
    Replies: 5
    Last Post: 01-09-2006, 12:40 PM