目录
一、auto
#include <iostream>
#include <typeinfo>
using std::cout;
using std::endl;
using std::cin;
auto max(int x, int y)
{
return x > y ? x : y;
}
int main()
{
//auto 变量必须在定义时初始化
auto x = 3;
auto y{
42 };
//定义在一个auto序列的变量必须始终推导成同一类型
auto x1{
1 }, x2{
2 };
//如果初始化表达式是引用或者const,泽去除引用或const语义
int y1{
42 }, & y2{
y1 };
auto y3{
y2 };
cout << typeid(y3).name() << endl;
//如果auto关键字带上&号,则不去除引用或const语意
auto& z1{
y2 };
cout << typeid(z1).name() << endl;
//初始化表达式为数组是,auto关键字推导类型为指针
int p[3]{
1,2,3 };
auto p1 = p;
cout << typeid(p1).name() << endl;
//若表达式为数组且auto带上&,则推导类型为数组类型
auto& p2{
p };
cout << typeid(p2).name() << endl;
//C++14中,auto可以作为函数的返回值类型和参数类型
cout << max(x1, x2) << endl;
cin.get();
return 0;
}
auto可以自行推断并且给出变量的类型,C++中应尽可能使用auto去定义。
二、declpyte
#include <iostream>
#include <typeinfo>
using namespace std;
int fun1() {
return 10; }
auto fun2() {
return 'g'; } //C++14
int main()
{
//Data type of x is same asreturn type of fun1()
//and type of y is same as return type of fun2()
decltype(fun1()) x;
decltype(fun2()) y=fun2();
cout << typeid(x).name() << endl;
cout << typeid(y).name() << endl;
cin.get();
return 0;
}
decltype在编译时期推导一个表达式的类型,而不用初始化,其语法格式有点像sizeof。