Article 5 of "Effective Modern C++" study notes: prefer auto instead of display type declaration

How much nonsense to make up, directly summarize the benefits of using auto:

1. For the uninitialized variable, if you use the display type declaration, the programmer will not perceive it, but if you use auto to declare the variable, it will remind you of a compilation error.

int x1;  //有潜在的未初始化风险
auto x2; //未初始化,编译报错

2. Simplify the code, especially when iterators are related, and can omit a large amount of code

3. For some lambda expressions, we should use auto variables to receive it, not std::function, because auto wins in terms of efficiency and code cleanliness

//应使用auto而非std::function
auto dereflens = [](const auto& p1,const auto& p2) { return *p1 > *p2}

4. Auto has better robustness during cross-system code migration. For example, the return value type of the size() function of the vector container of STL is actually std::vector<int>::size_type. Most people may not know this type. When writing code, we only use unsigned to accept the Function return value. But we need to know that in a 32-bit operating system, unsigned and std::vector<int>::size_type are both 32-bit, so they can be received directly like this, but in a 64-bit operating system, std::vector<int>:: The size_type is 64 bits, and if you still use unsigned (32 bits) to receive at this time, it will be out of range. If we use auto to declare, whether it is 32-bit or 64-bit, sz is the same size as std::vector<int>::size_type

//当前是32位
std::vector<int>
unsigned sz = v.size() //此时sz与std::vector<int>::size_type的大小都为32位


//当前是64位
std::vector<int>
unsigned sz = v.size() //此时sz是32位,std::vector<int>::size_type是64位,越界


但是如果我们使用auto,可以直接移植,无需关心

5. For some obscure knowledge points, it can play a good evasion effect. For example, if the following code does not use auto, it will cause a very secret problem:

std::unordered_map<std::string,int> m;

for(const std::pair<std::string,int>& p :m) {
   ...//一些操作
}

The above code ignores the fact that the key value part of std::unordered_map is const, so its member type is std::pair<const std::string,int> instead of std::pair<std::string ,int>, so the above code will occur std::pair<const std::string,int> -> std::pair<std::string,int> implicit type conversion, from const to non-const The method is to perform a copy operation, generate a temporary variable, and then bind p to this temporary variable, and at the end of a loop, this temporary variable will also be destroyed. It can be seen from the analysis that the above code is very wasteful in efficiency. But if we use auto, this problem can be completely avoided.

 

Shorthand

  • The auto variable must be initialized. Basically, it is immune to the type mismatch that can cause compatibility and efficiency problems. It can also simplify the reconstruction process, usually less typing than the specified type.
  • The auto type has the disadvantages described in terms 2 and 6

Guess you like

Origin blog.csdn.net/Chiang2018/article/details/114005645