auto和decltype

本文介绍了C++11引入的auto关键字和decltype运算符。auto用于类型推导,常忽略顶层const,但保留底层const。decltype则能精确反映表达式的原始类型,包括引用和const特性。在处理引用时,auto保持原类型,而decltype返回引用类型。对于指针解引用,auto仍保持原类型,decltype则返回引用。
摘要由CSDN通过智能技术生成

目录

auto

decltype

总结


auto

        C中的auto表示自动变量

        在C++(C++11)中他是类型推导,也就是更具数据自动确定其类型

auto num = 11 + 22;    //auto 是 int
auto num = 11.11 + 22.22    //auto是double

        对于const一般会忽略上层,保存底层,顶层的话可以手动添加

const int a = 4;
auto b = a;    //auto 是 int
auto c = &a;    //auto 是 const int *
const auto d = a;

        对于引用来说auto是原对象

int a = 11;
int &ra = a;
auto b = ra;    //auto 是int 并非&

decltype

        decltype可以推断出类型,原本是什么类型就是什么类型,对与引用和const也一样

int a = 11;
decltype(a) b;
decltype(a) c = 10;

        对于引用想要取指向类型的话

int a = 11;
int &ra = a;
declt
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值