Effective C ++ Item 30: Implementing the (thorough understanding of the ins and outs of inlining)

A, inline advantages and disadvantages

advantage

  • Eliminating the cost function call

Shortcoming

  • As a function of the body instead of function calls, so a large increase target
  • On a priority memory machines, excessive use of inline volume too much will cause the program
  • Even with virtual memory, inline code bloat caused also cause additional paging behavior, reduced instruction set of the cache device, and the accompanying loss of efficiency

Second, the implicit inline explicit inline

  • inline is only a request to the compiler, not mandatory command
  • Now only inside the class implicitly inline . E.g:
class Person {
public:
    int age()const { return theAge; } //隐式内联(编译器自动申请)
private:
    int theAge;
};
  • We can also point out explicitly by keyword inline a function as an inline function. E.g:
template<typename T>
inline const T& std::max(const T& a, const T& b)
{
    return a < b ? b : a;
}

Third, the template inline

  • inline functions are usually placed in the header files , because most build environment inlining in the compilation process, need to know the inline function looks like. inlining compiler of behavior in most C ++ programs (but there are a few cases inlining is done in the run-link period)
  • template usually is placed in the template header , so that once used, the compiler to be instantiated, but also need to know what it looks like
  • template of concrete and inlining has nothing to do:
    • If you write a function of the specific implementation of the template think should be inlining, then it will be declared as inline template
    • If you write code inlining is no reason why it should, then it will not be declared as a template inline (as may produce code bloat)

Fourth, the case of rejected by the compiler to inline

  • Even if you declare a function inline, but in some cases the compiler will refuse to function as inlining. E.g:
    • Too complicated functions : for example, with or recursive loop
    • Calls to virtual functions (unless it is the most prosaic): Because virtual means "wait" until the run was to determine which function to call, but inline means that it can be determined at compile call the function body. So virtual function will be refused delivery of the compiler to be inline
  • to sum up:
    • A seemingly function inline, or explicit function using inline declaration, in the end is not an inline function, depending on your environment and compiler
    • Most compilers provide a diagnostic level: If you can not inline a function of, will give a warning

The constructor and destructor are not sometimes the inlining

  • Now there following a class inheritance hierarchy
class Base{
public:
    //...
private:
    std::string bm1, bm2;
};

class Derived :public Base {
public:
    Derived() {}   //构造函数为空
private:
    std::string dm1, dm2, dm3;
};
  • The above Derived constructor is empty, then you might think when inlining the constructor of Derived, but in fact is not the case
  • We know that the C ++ constructor and destructor portion following rules:
    • If the class is derived, then the need to perform their own configuration before the base class constructor and destructor similar
    • If you do not initialize the data members of the class in the constructor, the compiler will automatically do the initial data members of a class (the initialization code compiler that you added)
  • The above example although Derived constructor is empty, but there are three data members, there are two base class data members. The following is pseudo-code, the compiler will automatically initializes the data members:
//伪代码
Derived::Derived() 
{
    //下面是编译器为空的Derived构造函数添加的代码
    Base::Base(); //初始化BaSE部分

    try {
        dm1.std::string::string();
    }
    catch (...) {
        Base::~Base();
        throw;
    }

    try {
        dm2.std::string::string();
    }
    catch (...) {
        dm1.std::string::~string();
        Base::~Base();
        throw;
    }

    try {
        dm3.std::string::string();
    }
    catch (...) {
        dm2.std::string::~string();
        dm1.std::string::~string();
        Base::~Base();
        throw;
    }
}

V. Summary

  • The most inlining is limited to small, frequently called the body function. This allows future debugging, and binary upgrades easier, but also allows potential problems to minimize code bloat, the opportunity to maximize program speedup
  • Do not, for function templates appear in the header file, they will be declared as private
Released 1504 original articles · won praise 1063 · Views 430,000 +

Guess you like

Origin blog.csdn.net/qq_41453285/article/details/104711068