Inline function

This is a discussion on Inline function within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a query regarding inline function. Where should they be define, in .cpp file or .h file and ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    104

    Inline function

    Hi,
    I have a query regarding inline function.
    Where should they be define, in .cpp file or .h file and what are benefits of doing that ??

    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,829
    Typically header files (.h).
    Declaring them as inline makes a suggestion to the compiler that it may remove the function altogether and put the code at the places where the function is called. It's called inlining and is performed at the whim of the compiler.
    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.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you use the function in several .cpp files, it should be in a header to have a good chance of being inlined - most compilers can only inline code that it has the source of. The latest MS Visual Studio has the ability to perform "Whole Program Optimization (WPO)", but most compilers can't do this. In WPO, the source code is only partially compiled at first, then the entire "partly compiled" source is made into machine code, and optimizations are done at this stage.

    gcc has been working on a similar scheme, but from what I understand, it's not yet complete.

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

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Code:
    #define CUBE(x) x*x*x
    Is this the idea of inline in C++? Also, is is merely for optimization?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Code:
    #define CUBE(x) x*x*x
    Is this the idea of inline in C++? Also, is is merely for optimization?
    No, that's a macro, not an inline.

    The inline version would be:
    Code:
    template <typename T>
    inline T  Cube(T x) {
       return x * x * x;
    }
    By using template, we can allow it to do all the things the above macro does, and inline should make the compiler insert the three multiplication operations directly where Cube() is being called, and as long as there is a multiplication

    Of course, if you know that Cube only takes (for example) double arguments, then :
    Code:
    inline double Cube(double x) 
    {
       return x * x  * x;
    }
    would be sufficient.


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

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,458
    Quote Originally Posted by csonx_p View Post
    Code:
    #define CUBE(x) x*x*x
    Is this the idea of inline in C++? Also, is is merely for optimization?
    No; that is a macro. Inlined functions work on the premise that, instead of calling a function, the compiler can put the function body inline at the point it is called. Of course, compiler writers often know more about code optimisation than programmers, so a compiler may choose to ignore a hint that it should inline a function.

    Incidentally, given your macro, you will find that CUBE(1+1) will yield 4. Whereas Cube(1+1) (calling the function described by matsp) will yield the expected value of 8.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by grumpy View Post
    No; that is a macro. Inlined functions work on the premise that, instead of calling a function, the compiler can put the function body inline at the point it is called. Of course, compiler writers often know more about code optimisation than programmers, so a compiler may choose to ignore a hint that it should inline a function.
    Not to mention that compiler writers know more about the limitations of when you can and can't inline a function for other reasons - e.g. a function that is called recursively would potentially become infinitely large when you inline it, so that prevents the function from being inlined.

    gcc 4 inlines REALLY LARGE functions if they are only called once, otherwise most compilers limit the size of inlined functions to avoid the code getting too big from copies of functions being inserted many places.

    Also, the compiler only takes the inline keyword itself as a hint - if the compiler thinks it's a good idea to inline a non-inline marked function, then it may well do so.

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

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,458
    Quote Originally Posted by matsp View Post
    Also, the compiler only takes the inline keyword itself as a hint - if the compiler thinks it's a good idea to inline a non-inline marked function, then it may well do so.
    True. In practice, however, inlining a non-inlined function requires more than a "run of the mill" C++ compiler. For example, on systems where the basic model is "compile to produce objects, and then link the objects to produce an executable", inlining of non-inlined functions often relies on help from the linker.

    Incidentally, JIT (Just In Time) techniques used in some virtual machines (eg with the Java VM, with some operating system emulators) are actually an example of inlining (and other associated code optimisations) being performed when the program is run.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by grumpy View Post
    True. In practice, however, inlining a non-inlined function requires more than a "run of the mill" C++ compiler. For example, on systems where the basic model is "compile to produce objects, and then link the objects to produce an executable", inlining of non-inlined functions often relies on help from the linker.
    You mean inlining of code produced in other modules (source files, compilation units, whatever you want to call it), yes that would require a different compiler structure than the traditional one. I was more referring to a concept where there is some function in the same source file, that is being called by another function.

    My point was more to that "inline" as a keyword is pretty much meaningless in a modern compiler for purposes other than "if you don't see any need for it, you don't need to give me an actual implementation of this function". This also applies to "static" functions - if they are not called or only called where they can be inlined [and the address isn't taken of the function somewhere], the compile can omit the code for that function in the resulting executable file.

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

  10. #10
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    I think your last question should be a new post....

    For a discussion about inlining functions, also recursive ones, see these two threads :

    Not inlined?
    inheritance and operator<<

    some nice information has been given by King Mir and brewbuck

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. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

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