typedef
typedef int size; // size 就代表int
typedef double wage, *p; // wage代表double,而p代表double*
typedef double adouble[10]; //adouble代表double[10]
在新标准中,可以用using
来替代它:
using p = double*;
有一点要注意:用了这个东西,标识符就是一个整体了,不能像define一样做字符串替换然后又拆开。比如:
typedef char * charp;
const charp cp = 0;
charp是一个整体,被const修饰。所以它实际是“常量指针”。要是拆开变成const char *
的话,就成为了“指向常量的指针”,这显然不对。
auto
auto i = 3 + 2; //从右推左, auto填充为int
auto m = 9。0, *p = &m; // auto填充为double
auto a = 3, b = 2.0; // 不可以,只能填充一个值
vector<int> v{1, 2, 3, 4};
auto it = v.begin(); // 类型为vector<int>::iterator,避免冗长的声明。
auto
是从右边类型推左边类型,并不是变量声明时的类型。
int i = 0, &r = i;
auto a = r; //a是int,不是int&
auto &b = r; //引用需显式指定
const int c = 0;
auto x = c; // 填充为int,右const被忽略
const auto y = c; // 右const要手动指定
auto p = &c; // 填充为const int *,左const会被保留
int ir[10];
auto x = ir; // 数组名被转化为指针,值int*
auto &x = ir; // 数组名还是数组,值为int[10]
auto也有助于提升程序的可拓展性,每个变量的类型都依赖其他变量,只需第一个声明的变量,后面所有变量的类型同时也就更改了,很方便。
auto a = 3ll;
auto i(a);
cout << typeid(i).name();
运行结果为 _int64 如果去掉3后面的ll 则为int类型
auto用于函数声明
在更新的版本中,auto还可以用于形参类型和返回值:
auto triple_adder(auto &&a, auto &&b, auto &&c) {
return a + b + c;
}
main() {
cout << triple_adder(1, 2, 3.2) << endl; //6.2
cout << triple_adder(string("he"), string("ll"), string("oo")) << endl; //helloo
}
将参数声明为auto
,作用与模板完全相同(但更简洁),会根据类型生成多个函数。而返回值设为 auto
,就可以根据return语句的类型自动生成返回类型。不过auto只是代表“自动推断一种类型”。C++的函数返回值只能有一种类型。如果多处return类型不一致,推断失败就会报错。
decltype
decltype
是和 auto 一起推出的,不过没那么简洁,用的不多。decltyle是完整严格的类型推断。
decltype(3) i = 12; // 3的类型是int,所以是int
decltype(f()) j = 0; // f的返回类型决定j的类型,但是并不会执行f
与auto
不同,decltype
的规则为“括号里面是什么就是什么”:
const int a = 3;
decltype(a) ca = a; //ca 是const int
int b = 5, &rb = b;
decltype(b) rb = a; //rb 是int&
decltype(b+0) rrb = 1; //是int
int ar[10];
decltype(ar) p; //是int[10]