C++ inline function of inline USES the details

  • 2020-04-02 02:20:13
  • OfStack

Before introducing inline functions, it's worth introducing preprocessor macros. The functionality of inline functions is similar to that of preprocessor macros. I'm sure you've all used preprocessor macros, and we'll often define macros such as


#define TABLE_COMP(x) ((x)>0?(x):0)

Defines a macro.

Why use macros? Because the call to a function must transfer the order of execution of the program to an address in memory where the function is stored, the program content of the function is executed, and then return to the place before the execution of the function. This transfer operation requires that the site be saved and the address of the execution be memorized before being transferred to the execution, and the site be restored after being transferred back to the original saved address to continue the execution. Therefore, function calls have some overhead in terms of time and space, which will affect their efficiency. Macros, on the other hand, simply expand the code where it is preprocessed, requiring no additional overhead in space or time, so calling a macro is more efficient than calling a function.
But macros also have a lot to be desired.
Macros cannot access private members of an object.
2. The definition of macros is easily ambiguous.
Here's an example:

#define TABLE_MULTI(x) (x*x)

We're going to call it with a number,TABLE_MULTI(10), and it's going to look like there's nothing wrong, and it's going to return 100, which is correct, but if we call it with TABLE_MULTI(10+10), we're going to expect 400, and the macro is going to call it (10+10*10+10), and it's going to be 120, which is obviously not what we're going to get. One way to avoid these errors is to bracket the arguments of a macro.


#define TABLE_MULTI(x) ((x)*(x))

This ensures that nothing will go wrong, but even with this definition, the macro can still go wrong, such as calling it with TABLE_MULTI(a++). What they really want is (a+1)*(a+1). We can look at the macro expansion result: (a++)*(a++), if a is 4, we get 5*6=30. And we're expecting 5 times 5 is 25, and that's a problem again. In fact, some of the C library functions have these problems as well. For example: Toupper(* char ++) performs the ++ operation twice on pChar, because Toupper is actually a macro as well.
We can see that macros have some unavoidable problems. How to solve them?
Here's how to solve these problems with the inline functions I'm going to introduce. Instead of defining macros, we can use inline functions. And in fact we can completely replace preprocessor macros with inline functions.
The difference between inline functions and macros is that macros are replaced by preprocessors, while inline functions are controlled by compilers. And the inline function is a real function, only in need of the time, the inline function like a macro expansion, so the elimination of the function of the parameters of the stack, reduce the call overhead. You can call inline functions just as you would a function without worrying about the problems that arise from handling macros.
We can use Inline to define Inline functions, however, any function defined in the description section of the class is automatically considered Inline.
Now let's introduce the use of inline functions.
An inline function must be declared with the body of the function to be valid. Declaring Inline Tablefunction(int I) like this has no effect, the compiler just declares the function as a normal function, we must define the body of the function.


Inline tablefunction(int I) {return I*I};

So we've defined an inline function. We can call it as a normal function. But it does execute faster than normal functions.
We can also define functions defined outside the class as inline functions, such as:


Class TableClass{
 Private:
Int I,j;
 Public:
Int add() { return I+j;};
Inline int dec() { return I-j;}
Int GetNum();
}
inline int tableclass::GetNum(){
return I;
}

All three functions declared above are inline. In C++, functions that define the body of a function within a class are inlined by default. Regardless of whether you have the inline keyword or not.
Inline functions are most commonly used in C++ classes to define access functions. The classes we define generally make the data members private or protected, so that the outside world cannot directly read or write the data of our class members. Private or protected members must be read and written using the member interface function. If we define these read-write member functions as inline functions, we will get better efficiency.


Class sample{
 Private:
Int nTest;
 Public:
Int readtest(){ return nTest;}
 Void settest(int I) {nTest=I;}
}

Of course, there are limitations to inline functions. You can't have too much executed code in a function, and if the body of an inlined function is too large, the normal compiler will abandon inlining and call the function the normal way. In this way, the inline function is as efficient as the normal function.


Related articles: