C++11 auto类型推导

1.类型推导

       C++11引入了auto 和 decltype 关键字实现类型推导,通过这两个关键字不仅能方便地获取复杂的类型,而且还能简化书写,提高编码效率。
     

auto 类型推导的语法和规则

       在之前的 C++ 版本中,auto 关键字用来指明变量的存储类型,它和 static 关键字是相对的。auto 表示变量是自动存储的,这也是编译器的默认规则,所以写不写都一样,一般我们也不写,这使得 auto 关键字的存在变得非常鸡肋。

C++11 赋予 auto 关键字新的含义,使用它来做自动类型推导。也就是说,使用了 auto 关键字以后,编译器会在编译期间自动推导出变量的类型,这样我们就不用手动指明变量的数据类型了。

auto 关键字基本的使用语法如下:

auto name = value;

name 是变量的名字,value 是变量的初始值。

注意:auto 仅仅是一个占位符,在编译器期间它会被真正的类型所替代。或者说,C++ 中的变量必须是有明确类型的,只是这个类型是由编译器自己推导出来的。

C11 中 auto 成为类型指示符(type-specifier)。

    auto类型推导: auto 定义的变量,可以根据初始化的值,在编译时推导出变量名的类型示例:

示例:

auto n = 10;
auto f = 12.8;
auto p = &n;
auto url = "http://c.linzeyu.net/cplus/";

第 1 行中,10 是一个整数,默认是 int 类型,所以推导出变量 n 的类型是 int。
第 2 行中,12.8 是一个小数,默认是 double 类型,所以推导出变量 f 的类型是 double。
第 3 行中,&n 的结果是一个 int* 类型的指针,所以推导出变量 p 的类型是 int*。
第 4 行中,由双引号""包围起来的字符串是 const char* 类型,所以推导出变量 url 的类型是 const char*,也即一个常量指针。

 

 auto 和 const 的结合:

int  x = 0;
const  auto n = x;//n 为 const int ,auto 被推导为 int
auto f = n;//f 为 const int,auto 被推导为 int(const 属性被抛弃)
const auto& r1 = x;//r1 为 const int& 类型,auto 被推导为 int
auto& r2 = r1;//r1 为 const int& 类型,auto 被推导为 const int 类型

第 2 行代码中,n 为 const int,auto 被推导为 int。
第 3 行代码中,n 为 const int 类型,但是 auto 却被推导为 int 类型,这说明当=右边的表达式带有 const 属性时, auto 不会使用 const 属性,而是直接推导出 non-const 类型。
第 4 行代码中,auto 被推导为 int 类型,这个很容易理解,不再赘述。
第 5 行代码中,r1 是 const int & 类型,auto 也被推导为 const int 类型,这说明当 const 和引用结合时,auto 的推导将保留表达式的 const 类型。

 

 

 结构体中:

 

 在结构体中的属性 只有int 类型能给出变量名和数据名

 数组名退化为一个指针,auto不能修饰数组

 推导返回类型

 

 

 返回值不同时无法推导:

 

 总结:
1、auto不能用于函数参数
2、auto不能用于非静态成员变量
3、 auto 无法定义数组
4、实例化模板时不能使用auto作为模板参数。 

示例:

int main()
{

    //根据初始值来推导   必须要有初始值
        auto x = 5; //x = int; auto int;
        auto ip = &x;  //ip= int*  auto int* 
        auto* s = new int(10); // s int*   auto int
        auto sp = new int(20);///sp= int*  auto int* 
        const auto* xp = &x; //xp const int*; auto int
        auto dx = 12.23;  //dx double
        auto s;   //err  s没有初始化
}


int main()
{
    auto a = 10, b = 20;
    auto cha = 'a', chb = 'b';

    auto x = 10;
    //const auto* xp=&x,u=0.6   err

}




int main()
{
    auto x = 0;//x = int; 
    auto* ip = &x;  // ip int*   auto int
    auto xp = &x;   // xp int*   auto int*

}


int main()
{
    auto x = 0;
    auto& c = x;   //c int &   auto int
    auto d = x;    //d int   auto  int;

    const auto cx = x;   //cx const int;
    auto f = cx;      //f=>  int
    auto& ff = cx;    //ff   const int&;   auto  const int
    f = 100;
   //err  ff = 200;


}


int main()
{
    const int a = 10;
    auto b = a;   //b的改变不会改变a值   int 
    //int& c = a;//error  c的改变会改变a  a是一个常性值 c为a的别名

    auto & c = a;//   const int

    auto* ip = &a;   //auto =>const int;
    const int* iip = &a;

}

1 不能推数组的类型:
2 结构体里面推数据成员的类型 

//auto:  
//1 不能推数组的类型:
//2 结构体里面推数据成员的类型
struct Node
{
    auto val;   //err
    int sum;
};



int main()
{
    auto ar[] = { 1,2,3,4,5 };
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值