auto keyword (C++11)

This blog will write a new feature auto in the C++11 standard and explain how to use it.

auto keyword

  • Introduction to auto

Introduction to auto: In C++11, auto is used as a new type indicator to instruct the compiler. Variables declared by auto must be deduced by the compiler at compile time . To put it simply, auto is equivalent to an automatic derivation .
In the early C/C++, the meaning of auto is: variables modified with auto are local variables with automatic memory .

int TestAuto()
{
    
    
  return 10;
}
int main()
{
    
    
  int a = 10;
  auto b = a; //推导出b为int
  auto c = 'a'; //推导出c为char
  auto d = TestAuto(); //和TestAuto返回的类型一样,即int
  cout << typeid(b).name() << endl;
  cout << typeid(c).name() << endl;
  cout << typeid(d).name() << endl;
 
//auto e; 无法通过编译,使用auto定义变量时必须对其进行初始化
 
  return 0;
}

Let me mention that typeid().name() is a function to get the variable type name. The running result is as follows.
Insert picture description here
Some people may think that auto is useless. Look at the following code:

#include<iostream>
#include<map>
using namespace std;

int main()
{
    
    
	std::map<std::string, std::string> dict;
	//假设不使用auto就需要写这一长串类型
	std::map<std::string, std::string>::iterator it = dict.begin();
	//使用auto后就方便很多
	auto it = dict.begin();
	
	cout << typeid(it).name() << endl;
	return 0;
}

The large string before it is the type name, which is much more convenient if you use auto.

  • Auto usage rules

1.
Auto is used in combination with pointers and references. When using auto to declare pointer types, there is no difference between auto and auto*, but when using auto to declare reference types, you must add &

int main()
{
    
    
  int x = 10;
  auto a = &x;
  auto* b = &x;
  auto& c = x;
  cout << typeid(a).name() << endl;
  cout << typeid(b).name() << endl;
  cout << typeid(c).name() << endl;
  return 0;
}

The code running results are as follows:
Insert picture description here

2. Define multiple variables
on the same line. When declaring multiple variables on the same line, these variables must be of the same type, otherwise the compiler will report an error because the compiler actually only
deduces the first type, and then uses the deduction The type that comes out defines other variables.

void TestAuto()
{
    
    
  auto a = 1, b = 2;
  //auto c = 3, d = 4.0; // 该行代码会编译失败,因为c和d的初始化表达式类型不同
  cout << typeid(a).name() << endl;
  cout << typeid(b).name() << endl;
}

The running result is also obvious:
Insert picture description here
3. Auto cannot be used as a function parameter.
TestAuto will create a stack frame on the stack when compiling, but you need to know the size of the space when opening up space. Auto a is a part of the stack frame. I don’t know how big the auto parameter is. , So the stack frame does not know how much space needs to be opened

// 此处代码编译失败,auto不能作为形参类型,因为编译器无法对a的实际类型进行推导
void TestAuto(auto a)
{
    
    }

4. Auto cannot be used directly to declare arrays.
Because arrays also involve size, in the following example, the type of a is strictly int [3], so the size of b is also uncertain.

void TestAuto()
{
    
    
  int a[] = {
    
    1,2,3};
  auto b[3] = a;
}

5. In order to avoid confusion with the auto in C++98, C++11 only retains the usage of auto as a type indicator.
6. The most common advantage of auto in practice is the new for loop provided by C++11 , And lambda expressions for use in conjunction.
7. Auto cannot define non-static member variables of the class
8. You cannot use auto as a template parameter when instantiating a template

Guess you like

Origin blog.csdn.net/NanlinW/article/details/103392859