• C++11新特性之三——auto/decltype


      C++11中引入的auto主要有两种用途:自动类型推断和返回值占位。auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除。前后两个标准的auto,完全是两个概念

    1. 自动类型推断

        auto自动类型推断,用于从初始化表达式中推断出变量的数据类型。通过auto的自动类型推断,可以大大简化我们的编程工作。下面是一些使用auto的例子。

    #include <vector>  
    #include <map>  
      
    using namespace std;  
      
    int main(int argc, char *argv[], char *env[])  
    {  
    //  auto a;                 // 错误,没有初始化表达式,无法推断出a的类型  
    //  auto int a = 10;        // 错误,auto临时变量的语义在C++11中已不存在, 这是旧标准的用法。  
      
        // 1. 自动帮助推导类型  
        auto a = 10;  
        auto c = 'A';  
        auto s("hello");  
      
        // 2. 类型冗长  
        map<int, map<int,int> > map_;  
        map<int, map<int,int>>::const_iterator itr1 = map_.begin();  
        const auto itr2 = map_.begin();  
        auto ptr = []()  
        {  
            std::cout << "hello world" << std::endl;  
        };  
      
        return 0;  
    };  
      
    // 3. 使用模板技术时,如果某个变量的类型依赖于模板参数// 不使用auto将很难确定变量的类型(使用auto后,将由编译器自动进行确定)。  
    template <class T, class U>  
    void Multiply(T t, U u)  
    {  
        auto v = t * u;  
    }  

    2.返回占位符:

    template <typename T1, typename T2>  
    auto compose(T1 t1, T2 t2) -> decltype(t1 + t2)  
    {  
       return t1+t2;  
    }  
    auto v = compose(2, 3.14); // v's type is double 

    3.使用注意事项:

    ①我们可以使用valatile,pointer(*),reference(&),rvalue reference(&&) 来修饰auto

    auto k = 5;  
    auto* pK = new auto(k);  
    auto** ppK = new auto(&k);  
    const auto n = 6;  

    用auto声明的变量必须初始化(auto需要根据初始化的值推断出类型)

    auto m; // m should be intialized 

    ③auto不能与其他类型组合连用

    auto int p; // 这是旧auto的做法。  

    函数和模板 参数 不能被声明为auto

    void MyFunction(auto parameter){} // no auto as method argument  
      
    template<auto T> // utter nonsense - not allowed  
    void Fun(T t){}  

    ⑤定义在 堆 上的变量,使用了auto的表达式必须被初始化(auto需要根据初始化的值推断出类型

    int* p = new auto(0); //fine  
    int* pp = new auto(); // should be initialized  
       
    auto x = new auto(); // Hmmm ... no intializer  
         
    auto* y = new auto(9); // Fine. Here y is a int*  
    auto z = new auto(9); //Fine. Here z is a int* (It is not just an int)  

    auto是一个占位符,并不是一个它自己的类型,因此不能用于类型转换或其他一些操作,如sizeof和typeid.

    int value = 123;  
    auto x2 = (auto)value; // no casting using auto  
      
    auto x3 = static_cast<auto>(value); // same as above   

    ⑦定义在一个auto序列的变量必须始终推导成同一类型

    auto x1 = 5, x2 = 5.0, x3='r';  // This is too much....we cannot combine like this  

    auto不能自动推导成CV-qualifiers(constant & volatile qualifiers),除非被声明为引用类型

    const int i = 99;  
    auto j = i;       // j is int, rather than const int  
    j = 100           // Fine. As j is not constant  
      
    // Now let us try to have reference  
    auto& k = i;      // Now k is const int&  
    k = 100;          // Error. k is constant  
    
    // or
    const auto k = i;
    k = 100    // Error. k is constant int 
    
    // or
    auto k = &i;
    *k = 100;   // Error. k is const int*
      
    // Similarly with volatile qualifer

    auto会退化成指向数组的指针,除非被声明为引用

    int a[9];  
    auto j = a;  
    cout<<typeid(j).name()<<endl; // This will print int*  
      
    auto& k = a;  
    cout<<typeid(k).name()<<endl; // This will print int [9]  

     

    =======================================================================

    decltype

    用于编译时推断类型,decltype有点像auto的逆过程:
    • 使用auto可以声明一个变量;
    • 而decltype可以通过一个变量得到类型。
    在C++11可以使用trailing-return-type指定返回值类型,而在C++14中可以使用decltype(auto)这种更强大的语法,让程序员不再纠结返回值类型。
    decltype 最主要用来声明一个函数模板,并且返回值的类型取决于参数的类型;(如上面第2点:返回占位符)

     

  • 相关阅读:
    Web项目发布步骤总结
    Web项目发布的一些设置
    阿里云部署Web项目
    Tomcat、Weblogic、WebSphere、JBoss服务器的选择
    EJB到底是什么
    数据库线程池3——现实中的参数
    数据库线程池2——线程池调优
    数据库线程池1——三种线程池的参数详细
    Mysql分页优化
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cardSalDao' defined in file [E:GItUppointerCard+redis argetgameCard-1.0-SNAPSHOTWEB-INFclassescnjbitdao
  • 原文地址:https://www.cnblogs.com/yyxt/p/4203871.html
Copyright © 2020-2023  润新知