从零单排《C++ Primer》
——(5)const,typedef,auto,decltype
CONST
多文件下
//file_1.cc 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.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 constconst int *p2 = &ci;//point to a const object
const 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't
p2 = p3; //ok:p2 has the same low-level const qualification as p3
p2 = &i; // ok:we can convert int* to const int*
int &r = ci;//error:can;t bind an ordinary int& to a const int object
const 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; //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 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的值要在运行时才能初始化,为了解决这个问题,新标准引入了constexprconstexpr 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 int
auto 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 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 reference
decltype(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;