C++ articles----auto, inline functions, function overloading


One, auto

Auto in C++ will automatically deduce the type on the left side of the equal sign according to the expression on the right side of the equal sign

#include<iostream>
using namespace std;

int main()
{
    
    
	int a = 0;
	auto b = a;

	double c = 1.1;
	auto d = c;

	//打印类型typeid().name()
	cout << typeid(b).name() << endl;
	cout << typeid(d).name() << endl;

	return 0;
}

insert image description here
Under what circumstances will auto be used?

auto is suitable for cases where the type of the expression on the right is very long, which is equivalent to the replacement of the type

In another case, when accessing the range for array, it must be judged every time, and auto increment or adjustment is used

for(auto e: arr) in this way, the array data can be assigned to e in turn, and then the obtained value can be inserted into cout and displayed on the console: it will
automatically iterate and automatically judge the end, but: only for arrays, others will not work

#include<iostream>
using namespace std;

int main()
{
    
    
	int arr[] = {
    
     1,2,3,4,5 };
	//这样就可以访问了,但是这样要修改数组内容应该如何办到
	for (auto e : arr)//根据左边类型自动为e匹配类型,且:将数组值依次赋值给e
		cout << e << ' ' ;
	cout << endl;

	/*for (auto e : arr)
		e *= 2;
	for (auto e : arr)
		cout << e << ' ';*/
	//在auto后面加引用操作符&,auto为变量e自动匹配类型,&就是对数组每个元素的引用,改变e就是改变数组

	/*for (auto& e : arr)//引用修改数组值
		e *= 2;
	for (auto e : arr)
		cout << e << ' ';*/
	return 0;
}

insert image description here

Two, inline function (inline)

When calling a function, it is necessary to create a stack frame, open up a space on the stack, push the stack, and use a lot of registers to copy and copy temporary variables, which has a certain consumption. For the function that is called repeatedly, this consumption is particularly obvious
. When a function is called once, a stack frame will be created. When a function is called n times, n stack frames will be created.
Building these stack frames consumes too much, so how can we reduce or reduce the cost of creating stack frames? to consume

In C language, macro functions can be used to implement functions that are called repeatedly

#include<iostream>
using namespace std;
//宏函数实现,宏中的函数参数没有给其声明类型,且没有返回(return)这个关键字,
//除了函数名之外的所有变量每个变量都要用括号括起来
#define Add(x,y) ((x)+(y))

int main()
{
    
    
	for (int i = 0; i < 10000; i++)
		Add(1, 2);
	return 0;
}

But not all are suitable for macros, and macros also have their advantages and disadvantages

Advantages: There is no need to create a stack frame when calling a function, which improves the calling efficiency. When a certain value in the function is needed, it can be modified directly in the macro without going to the function to modify one by one. Disadvantages: Complex, error-prone, and poor
readability (For a function, you don’t know what type the parameter is when you read it), and you cannot debug

So how does c++ solve it?

For such a large number of repeated calls to a function, c++ uses inline functions

insert image description here

Inline function: It is to expand the function at the place where the function is called, unlike ordinary functions that need to create a stack
frame expands the function, reducing the need for building a stack frame. However,
expanding this function requires additional space. This method trades space for time.
When expanding, if the amount of code in the function is too large, the executable program will become larger and the code will bloat
. Linked functions are suitable for functions that are called repeatedly and have a small amount of code (generally no more than ten lines of code)

So which functions does inlining not work with?

There is a lot of code in the function
Recursive function

Although we use inline to make the function an inline function, we can make any function an inline function for us, but this is just our suggestion to the compiler. As for whether to adopt it, it depends on whether the compiler can become an inline function for this function. Linked judgment, if there is too much code in the function or the recursive function, the compiler will not adopt it

And also note that the declaration and definition of inline functions cannot be separated, because the function is expanded when it is declared, if it is defined separately, at this time, it is called to find its address when linking, and it cannot be found at all.
insert image description here

3. Function overloading

Function overloading: It is equivalent to the meaning of polysemy in Chinese. Sometimes a Chinese character has multiple meanings
and function overloading is also polysemy. Although a function name realizes the function NVC, but Its specific parameters are different.
The parameters are different (parameter type, parameter number, parameter type order). Multiple functions are similar in function implementation, but there are some differences in specific details. The same function name
means that the function implementation function is similar, but There is a difference in the data type of the formal parameter c++ allows the same function name

The C language does not allow the same function name to appear. When the function has the same name, it is not known who is called when calling the function.

Parameter Type

#include<iostream>
using namespace std;

//参数类型不同
void fun(int a)
{
    
    
	cout << "fun(int a)" << endl;
}

void fun(double a)
{
    
    
	cout << "fun(double a)" << endl;
}

int main()
{
    
    
	fun(1);
	fun(1.2);

	return 0;
}


Number of parameters

#include<iostream>
using namespace std;

参数个数不同
void fun(int a, int b)
{
    
    
	cout << "fun(int a,int b)" << endl;
}

void fun(int a)
{
    
    
	cout << "fun(int a)" << endl;
}

int main()
{
    
    
	fun(1,2);
	fun(1);

	return 0;
}

insert image description here

Parameter type order

#include<iostream>
using namespace std;
//参数类型顺序不同
void fun(int a, double b)
{
    
    
	cout << "fun(int a, double b)" << endl;
}

void fun(double a, int b)
{
    
    
	cout << "fun(double a, int b)" << endl;
}

int main()
{
    
    
	fun(1, 1.1);
	fun(1.1, 1);

	return 0;
}


insert image description here

insert image description here

Whether the default parameters of the function can constitute overloading, constitute, but if the two functions pass nothing when calling the function to pass parameters, it does not constitute function overloading, and there is ambiguity in the call

When the C language function is called, the call instruction only converts it into a function address name.
The C language does not support function overloading. Because when linking, the address of the function is called, and the same function name is called. At this time, there are multiple addresses for the call. The
compiler does not know which address to look for, so the C language does not support function overloading. The c language does not have a function name modification rule, and the function name is not changed when the function is compiled and called.
Therefore, when linking to find the address (looking for the function name), because there are multiple addresses, when a call does not know what to look for Which address, call is ambiguous
insert image description here

In c++, when calling the stack, the address of each function is different. After using the same function to push the stack, their function names will change. This change follows the function name modification rules. When calling a function, under
linux C++ brings the parameter name into the function name, generally prefixed with _Z and then _Z+ number (the number of characters in the original function name) + the abbreviation of the parameter type of the calling function, and the number of characters after that indicates the formal parameter
type How many are there? The type is abbreviated as the first character of the type.
Find the function name when looking for its address when linking. At this time, their function names are different. Of course, they can be called. If each function name is different, there will be no call ambiguity
. Modification rules: bring function parameters into the function name
insert image description here
insert image description here
Under linux, c++ brings the parameter type into the function name, all of which is why there is no call ambiguity when calling

Guess you like

Origin blog.csdn.net/m0_67768006/article/details/130335811