Benefits of inline functions in C++?

后端 未结 14 1617
终归单人心
终归单人心 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.

提交回复
热议问题