• Effective Modern C++[实践]->理解auto类别推导


    1. 一般情况,auto推导与模板类型推导一致,唯一不同是auto可使用{}进行推导,模板不行
    2. c++14在函数返回值或者lambda形参中使用auto时,使用的是模板推导而不是auto推导,因此{}使用需注意
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    
    using namespace std;
    
    void someFunc(){
    
    }
    template<typename T>
    void f0(T value){
    
    }
    
    template<typename T>
    void f(std::initializer_list<T> value){
    
    }
    int main() {
        auto x = 5;            
        const auto cx = x; //既非指针也非引用   
        const auto &rx = x; //const 引用
      	const auto *ptx = &x;//const 指针
        auto &rx1 = x; //引用
      	auto *ptx1 = &x;//指针
      
      
        auto&& uref1 = 5;   //右值 
      	auto&& uref2 = x;   // x是左值,
        auto&& uref3 = cx;   
        auto&& uref4 = rx;  
    
        auto func1 = someFunc; //退化为函数指针
        auto& func2 = someFunc;  // 函数引用
      
      	const char text[]="xi men 吹雪";
      	auto arr1 = text;//退化为指针
      	auto & arr2 = text;//带长度的数组
      
       //c98语法
      	auto x1 = 27;
      	auto x2(27);
      //c11语法
      	auto x3={27};
      	auto x4{27};
       //c11 语法
      	auto x5={1,2,3};
        //error: deduced conflicting types ('int' vs 'double') for initializer list element typeauto x6={1,2,3.1};
    	// auto x6={1,2,3.1};
      
      	f({1,2,3,4});
      //auto与模板推导的唯一区别:模板无法使用{}
      //f1({11});//error: use of undeclared identifier 'f1' f1({11});
      //f1({11,2});//error: use of undeclared identifier 'f1' f1({11,2});
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    编译生成的代码如下:

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    
    using namespace std;
    
    void someFunc()
    {
    }
    
    template<typename T>
    void f0(T value){
    
    }
    
    template<typename T>
    void f(std::initializer_list<T> value){
    
    }
    
    /* First instantiated from: insights.cpp:51 */
    #ifdef INSIGHTS_USE_TEMPLATE
    template<>
    void f<int>(std::initializer_list<int> value)
    {
    }
    #endif
    
    int main()
    {
      int x = 5;
      const int cx = x;
      const int & rx = x;
      const int * ptx = &x;
      int & rx1 = x;
      int * ptx1 = &x;
      int && uref1 = 5;
      int & uref2 = x;
      const int & uref3 = cx;
      const int & uref4 = rx;
      using FuncPtr_33 = void (*)();
      FuncPtr_33 func1 = someFunc;
      void (&func2)() = someFunc;
      const char text[14] = "xi men \345\220\271\351\233\252";
      const char * arr1 = text;
      char const (&arr2)[14] = text;
      int x1 = 27;
      int x2 = 27;
      std::initializer_list<int> x3 = std::initializer_list<int>{27};
      int x4 = {27};
      std::initializer_list<int> x5 = std::initializer_list<int>{1, 2, 3};
      f(std::initializer_list<int>{1, 2, 3, 4});
      return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    auto推导
    auto x = 5;int x = 5;
    const auto cx = xconst int cx = x;
    const auto *ptx = &xconst int * ptx = &x;
    auto &rx1 = x;int & rx1 = x;
    auto *ptx1 = &x;int * ptx1 = &x;
    auto&& uref1 = 5;int && uref1 = 5;
    auto&& uref2 = x;int & uref2 = x;
    auto&& uref3 = cx;const int & uref3 = cx;
    auto&& uref4 = rx;const int & uref4 = rx;
    auto func1 = someFunc;using FuncPtr_33 = void (*)();FuncPtr_33 func1 = someFunc;
    auto& func2 = someFunc;void (&func2)() = someFunc;
    auto arr1 = text;const char * arr1 = text;
    auto & arr2 = text;char const (&arr2)[14] = text;
    auto x3={27};std::initializer_list x3 = std::initializer_list{27};
  • 相关阅读:
    【SQL注入点】注入点出现位置、判断
    TC8:UDP_INTRODUCTION_01-03
    原画设计咨询回复话术
    二元非洲秃鹫优化算法(Matlab代码实现)
    kr 第三阶段(一)16 位汇编
    【UE5 C++基础 04】UHT基础
    【数据结构】原来你叫“带头结点的双向循环链表”啊
    【论文阅读】Extract Free Dense Labels from CLIP
    论文的章节有重复率的要求吗?
    4.1提出问题&4.2拉格朗日插值
  • 原文地址:https://blog.csdn.net/MMTS_yang/article/details/125486654