【C++注意事项】5 Top-level const , The auto and decltype Type Specifier

本文详细介绍了C++中顶级const的概念,它用于表明指针本身是常量。同时,讲解了auto类型指定符如何让编译器自动推断变量类型,以及在不同情况下对顶层const的处理。接着,讨论了decltype类型指定符,它能从表达式中获取类型但不执行表达式。最后,文中还探讨了decltype与引用之间的交互及其特殊情况。
摘要由CSDN通过智能技术生成

top-level const

As we’ve seen, a pointer is an object that can point to a different object. As a result, we can talk independently about whether a pointer is const and whether the objects to which it can point are const. we use the top-level const to indicate that the pointer itself is a const. When a pointer can point to a const object, we refer to that const as a low-level const.

More generally, top-level const indicates that an object itself is const. Top-level const can appear in any object type,i.e., one of the built-in arithmetic types, a class type, or a pointer type. Low-level const appears in the base type of compound types such as pointer or reference. Note that pointer types, unlike most other type, can have both top-level and low-level const independently:

int i= 0;
int *const p1= &i;  // we can't change the value of p1;const is top-level
const int ci= 42;  // we can't change ci;const is top-level
const int *p2= &ci;  // we can't change p2;cosnt is low-level
const int *const p3= p2;  // right-most const is top-level,left-most is not
const int &r= ci;  // const in reference types is always low-level

The auto Type Specifier

It is not uncommon to want to store the value of an expression in a variable. To declare the variable, we have to know the type of that expression. When we write a program, it can be surprisingly difficult–and sometimes even impossible–to determine the type of an expression. Under the new standard, we can let the compiler figure out the type for us by using the auto type specifier. Unlike type specifiers, such as double, that names a specifier type, auto tells the compiler to deduce the type from the initi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值