(5)风色从零单排《C++ Primer》 const,typedef,auto,decltype
来源:互联网 发布:linux 查看关机原因 编辑:程序博客网 时间:2024/04/29 07:36
从零单排《C++ Primer》
——(5)const,typedef,auto,decltype
CONST
多文件下
//file_1.cc defines and initializes a const that is accessible to other filesextern const int bufSize = fcn();//file_1.hextern const int bufSize;//same bufSize as defined in file_1.cc
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 objectconst int &r = ci;//const in reference types is always low-level
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; //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-level
Exercise 2.31: int i = 0; 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 r1 = v2;//ok p1 = p2; //error v2 is top-level p2 = p1;//ok p1 = p3; //error p3 is top-level p2 = p3;//ok
Constant 表达式以及 consterxpr
constant表达式必须满足以下两个条件:
1)不被改变
2)在编译时便可估算
const int max_files = 20;//constant expressionconst int limit = max_files + 1; //limit is a constant expressionint staff_size = 27;//not a constant expressionconst int sz = get_size();//not a constant expressionsz的值要在运行时才能初始化,为了解决这个问题,新标准引入了constexpr
constexpr int mf = 20; //20 is a constant expressionconstexpr int limit = mf + 1;mf + 1 is a constant expressionconstexpr 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 cahrconst 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 dropped auto c = cr; auto d = &i; auto e = &ci; const auto f = ci; auto &g = ci; const auto &j = 42; a = 42; //ok b = 42; //ok c = 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;//int const auto &k = i;//const int auto *p = &i;//const int * const auto j2 = i,&k2 = i;//const int, reference to const
decltype
decltype和auto累西,也是一种推断类型,但是它不需要像auto一样采取赋值初始化的方式来。可以推断出top-level const
decltype(f()) sum = x;// sum has whatever type f returnconst 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 int decltype((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;
0 0
- (5)风色从零单排《C++ Primer》 const,typedef,auto,decltype
- (1)风色从零单排《C++ Primer》 一个简单的c++程序
- (4)风色从零单排《C++ Primer》 变量,引用,指针
- (7)风色从零单排《C++ Primer》 string
- (2)风色从零单排《C++ Primer》 一个简单的书店程序
- (3)风色从零单排《C++ Primer》 重要的基本类型
- (6)风色从零单排《C++ Primer》 结构体,头文件
- 从零单排c++ primer(5)
- C++primer---constexpr、类型别名、auto、decltype
- 从零单排5
- 从零单排c++ primer(1)
- 从零单排c++ primer(2)
- 从零单排c++ primer(3)
- 从零单排c++ primer(4)
- 从零单排c++ primer(6)
- 从零单排c++ primer(7)
- 从零单排c++ primer(8)
- 从零单排c++ primer(9)
- Android 注解和反射原理和实现学习(上)
- Spring整合DWR
- SpringMVC+MyBatis简单学习
- Algorithms—119.Pascal's Triangle II
- 分块矩阵求逆不能想当然
- (5)风色从零单排《C++ Primer》 const,typedef,auto,decltype
- C语言学习笔记 之 结构体指针变量
- 理解Java中的String数据类型
- GlusterFS集群文件系统研究
- linux线程间同步(1)读写锁
- Leetcode|Repeated DNA Sequences
- 通过分析 JDK 源代码研究 TreeMap 红黑树算法实现
- 程序员常去的14个顶级开发社区
- UI界面并不是只有白色设计才叫极简设计