admin 管理员组文章数量: 887021
(5)风色从零单排《C++ Primer》 const,typedef,auto,decltype
从零单排《C++ Primer》
——(5)const,typedef,auto,decltype
CONST
多文件下
//file_1 defines and initializes a const that is accessible to other files
extern const int bufSize = fcn();//file_1.h
extern const int bufSize;//same bufSize as defined in file_1
Exercuse 2.26 Which of the following are legal?For those that are illegal,explain why.
(a)const int buf; //error must be initialized
(b)int cnt = 0; //ok
(c)const int sz = cnt; //ok
(d)++cnt;++sz; // error sz is read-only
const引用以及指针
Top-Level const
int i =0;
int *const p1 = &i; const int ci = 42;
Low-Level const
const int *p2 = &ci;//point to a const object
const int &r = ci;//const in reference types is always low-level
low与top levelconst之间的复制会受到限制
const int *const p3 = p2;int *p = p3;//error:p3 has a low-level const but p doesn'tp2 = p3; //ok:p2 has the same low-level const qualification as p3p2 = &i; // ok:we can convert int* to const int*int &r = ci;//error:can;t bind an ordinary int& to a const int objectconst int &r2 = i; // ok:can bind const int& to plain int
Exercise 2.30 : For each of the following declarations indicate whether the object being declared has top-level or low-level const.const int v2 = 0; //top
int v1 = v2; //nonconst
int *p1 = &v1,&r1 = v1;//noneconst
const int *p2 = &v2, *const p3 = &i, &r2 = v2;//p2 is low-level,p3 is top-level, r2 is low-level
Exercise 2.31:int i = 0;const int v2 = 0; //topint v1 = v2; //nonconstint *p1 = &v1,&r1 = v1;//noneconstconst int *p2 = &v2, *const p3 = &i, &r2 = v2;//p2 is low-level,p3 is top-level, r2 is low-levelr1 = v2;//okp1 = p2; //error v2 is top-levelp2 = p1;//okp1 = p3; //error p3 is top-levelp2 = p3;//ok
Constant 表达式以及 consterxpr
constant表达式必须满足以下两个条件: 1)不被改变 2)在编译时便可估算
const int max_files = 20;//constant expression
const int limit = max_files + 1; //limit is a constant expression
int staff_size = 27;//not a constant expression
const int sz = get_size();//not a constant expression
sz的值要在运行时才能初始化,为了解决这个问题,新标准引入了constexpr constexpr int mf = 20; //20 is a constant expression
constexpr int limit = mf + 1;mf + 1 is a constant expression
constexpr int sz = size();//ok only if size is a constexpr function
constexpr int *q = nullptr;//q is a const pointer to int
typedef
使用typedef可以给类型一个新的名称,以方便使用。typedef char *pstring;
const pstring cstr = 0;//cstr is a constant pointer to cahr
const pstring *ps;//ps is a pointer to a constant pointer to char
在新标准下,可以使用using using SI = Sales_item;
auto
auto是一种推断类型。最多只能推断出low-level const。auto i =0, *p = &i;// ok i is int and p is a pointer to intauto sz = 0,pi = 3.14;//error inconsistent types for sz and pi
//Exercise 2.33 Using the variable definitions from this section,determine what happens in each of these assignmentss:int i =0, &r = i;auto a = r;const int ci = i, &cr = ci;auto b = ci; //top-level const in ci is droppedauto c = cr;auto d = &i;auto e = &ci;const auto f = ci;auto &g = ci;const auto &j = 42;a = 42; //okb = 42; //okc = 42; //ok//d = 42; error int to int *//e = 42; error int to int *//g = 42; error read-only
//Exercise 2.35 Determine the types deduced in each of the following definitions.Once you've figured out the types, write a program to see whether you were correct.const int i =42;auto j = i;//intconst auto &k = i;//const intauto *p = &i;//const int *const auto j2 = i,&k2 = i;//const int, reference to const
decltype
decltype和auto累西,也是一种推断类型,但是它不需要像auto一样采取赋值初始化的方式来。可以推断出top-level constdecltype(f()) sum = x;// sum has whatever type f return
const int ci = 0;
decltype(ci) x = 0;//x has type const int
decltype((i)) d;// error remember that decltype((variable)) is always a reference type, but decltype(variable) is a reference type only if variable is a referencedecltype(i) e; // ok e is an (uninitialized) int
Exercise 2.36:
int a = 3, b = 4;decltype(a) c = a;// c is intdecltype((b)) d = a;//d is reference++c;++d;
Exercise 2.37:int a = 3, b = 4;decltype(a) c = a;decltype(a = b) d = a;//d也是引用,返回的事表达式左边的操作数的地址,也既是int &++c;++d;
本文标签: (5)风色从零单排《C Primer》 const typedef auto decltype
版权声明:本文标题:(5)风色从零单排《C++ Primer》 const,typedef,auto,decltype 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1693849301h245781.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论