Benefits of inline functions in C++?

后端 未结 14 1404
终归单人心
终归单人心 2020-11-22 05:30

What is the advantages/disadvantages of using inline functions in C++? I see that it only increases performance for the code that the compiler outputs, but with today\'s opt

相关标签:
14条回答
  • 2020-11-22 05:58

    In archaic C and C++, inline is like register: a suggestion (nothing more than a suggestion) to the compiler about a possible optimization.

    In modern C++, inline tells the linker that, if multiple definitions (not declarations) are found in different translation units, they are all the same, and the linker can freely keep one and discard all the other ones.

    inline is mandatory if a function (no matter how complex or "linear") is defined in a header file, to allow multiple sources to include it without getting a "multiple definition" error by the linker.

    Member functions defined inside a class are "inline" by default, as are template functions (in contrast to global functions).

    //fileA.h
    inline void afunc()
    { std::cout << "this is afunc" << std::endl; }
    
    //file1.cpp
    #include "fileA.h"
    void acall()
    { afunc(); }
    
    //main.cpp
    #include "fileA.h"
    void acall();
    
    int main()
    { 
       afunc(); 
       acall();
    }
    
    //output
    this is afunc
    this is afunc
    

    Note the inclusion of fileA.h into two .cpp files, resulting in two instances of afunc(). The linker will discard one of them. If no inline is specified, the linker will complain.

    0 讨论(0)
  • 2020-11-22 06:01

    Why not make all functions inline by default? Because it's an engineering trade off. There are at least two types of "optimization": speeding up the program and reducing the size (memory footprint) of the program. Inlining generally speeds things up. It gets rid of the function call overhead, avoiding pushing then pulling parameters from the stack. However, it also makes the memory footprint of the program bigger, because every function call must now be replaced with the full code of the function. To make things even more complicated, remember that the CPU stores frequently used chunks of memory in a cache on the CPU for ultra-rapid access. If you make the program's memory image big enough, your program won't be able to use the cache efficiently, and in the worst case inlining could actually slow your program down. To some extent the compiler can calculate what the trade offs are, and may be able to make better decisions than you can, just looking at the source code.

    0 讨论(0)
  • 2020-11-22 06:03

    inline allows you to place a function definition in a header file and #include that header file in multiple source files without violating the one definition rule.

    0 讨论(0)
  • 2020-11-22 06:04

    Inlining is a suggestion to the compiler which it is free to ignore. It's ideal for small bits of code.

    If your function is inlined, it's basically inserted in the code where the function call is made to it, rather than actually calling a separate function. This can assist with speed as you don't have to do the actual call.

    It also assists CPUs with pipelining as they don't have to reload the pipeline with new instructions caused by a call.

    The only disadvantage is possible increased binary size but, as long as the functions are small, this won't matter too much.

    I tend to leave these sorts of decisions to the compilers nowadays (well, the smart ones anyway). The people who wrote them tend to have far more detailed knowledge of the underlying architectures.

    0 讨论(0)
  • 2020-11-22 06:04

    During optimization many compilers will inline functions even if you didn't mark them. You generally only need to mark functions as inline if you know something the compiler doesn't, as it can usually make the correct decision itself.

    0 讨论(0)
  • 2020-11-22 06:04

    Our computer science professor urged us to never use inline in a c++ program. When asked why, he kindly explained to us that modern compilers should detect when to use inline automatically.

    So yes, the inline can be an optimization technique to be used wherever possible, but apparently this is something that is already done for you whenever it's possible to inline a function anyways.

    0 讨论(0)
提交回复
热议问题