C ++ basics 003 inline function topic

inline function topic

/ *
 * Operating platform: Visual Studio 2015
 * Reference: "C ++ Primer Plus (Sixth Edition)", Chuanzhi sweeping C ++ basic courses
* /


I. Introduction

Before talking about inline functions, let ’s ask everyone a
  question : const constants in C ++ can replace macro constant definitions to a certain extent, so is there a solution to replace macro code fragments?
  Answer: Certainly, after all, C ++ is an upgrade to C. In C ++, it is recommended to use inline functions instead of macro code fragments .

Two: So how to use it?

   Use the inline keyword to declare inline functions in C ++. The inline keyword must be combined with the function definition when it is declared , otherwise the compiler will directly ignore the inline request.如:inline void get();

3. Attention

1. There is no definition in the code generated by the inline function

  The C ++ compiler inserts the function body directly at the place where the function is called. Inline functions do not have the extra overhead of normal function calls (pushing, jumping, and returning).
  Therefore, when the execution cost of the function body is much greater than the cost of pushing the stack, jumping and returning, then inlining will be meaningless.

inline void get()
{
	printf("Hello\n");
} 
int main()
{
	get();
	/* 相当于 */
	{
		printf("Hello\n");
	}
}

2. The C ++ compiler does not necessarily permit inline requests for functions

  Inline functions are special functions that have the characteristics of ordinary functions (parameter checking, return type, etc.). The inline function is a request to the compiler . Since it is a request, it can certainly be rejected, so the compiler may reject this request .

3. The difference between inline and macro code fragments

  The inline function is processed by the compiler, and the compiled function body is directly inserted into the calling place.
  Macro code fragment by a treatment pre-processor , a simple text replacement , without any compilation process .

4. C ++ compiler can compile and optimize

  Modern C ++ compiler to compile optimized, thus some functions even without inline statement , could be compiler linking compiled within .

5. Limitations of inline compilation in C ++

  • Not able to have any form of loop
  • Not be able to exist too many conditional statements
  • The function body is not able too large
  • Not able to perform the function address- operation
  • Function inline declaration must be before the calling statement

6. Inline substitution of macro code fragments can avoid the side effects of macros

Use a program to illustrate:

#include "iostream"
using namespace std;

#define MYFUNC(a, b) ((a) < (b) ? (a) : (b))  
inline int myfunc(int a, int b) 
{
	return a < b ? a : b;
}

int main()
{
	int a = 1;
	int b = 3;
	
	int c = myfunc(++a, b); 
	printf("a = %d\n", a); 
	printf("b = %d\n", b);
	printf("c = %d\n", c);
	
	c = MYFUNC(++a, b);  
	printf("a = %d\n", a); 
	printf("b = %d\n", b);
	printf("c = %d\n", c);

	system("pause");
	return 0;
}

Results: You can see that the results of the two executions are inconsistent , so using #define to define code blocks may cause certain side effects.
Insert picture description here
Explanation:
Insert picture description here

in conclusion:

  1. Inline functions insert the function body directly into the place where the function is called at compile time.
  2. Inline is just a request, the compiler does not necessarily allow this kind of request.
  3. Inline functions eliminate the overhead of stacking, jumping, and returning when ordinary functions are called.
Published 41 original articles · won 31 · views 3681

Guess you like

Origin blog.csdn.net/weixin_42813232/article/details/105478535