在c++11中我们可以使用 auto自动推导变量的类型,还可以结合declttype来表示函数的返回值
一、auto关键字
(一)auto的推导规则
在c++11中auto并不代表一种实际的数据类型只是一个类型声明的"占位符"
- auto在使用时必须对auto声明的变量初始化,从而让编译器推导出他的示例类型,在编译时自动将auto替换为真正的数据类型
auto 变量名 = 变量值;
#include <iostream>
using namespace std;
void test(){
auto x=10;
auto y="hello world";
auto z=true;
cout<<typeid(x).name()<<endl;
cout<<typeid(y).name()<<endl;
cout<<typeid(z).name()<<endl;
int temp =20;
auto *a = &temp;
auto b=&temp;
cout<<typeid(a).name()<<endl;
cout<<typeid(b).name()<<endl;
}
int main() {
test();
system("pause");
return 0;
}

- auto可以和指针、引用结合起来使用 也可以带上 const 、volatile限定符
- 当变量不是指针或者引用类型时,推导的结果不会保留 const volatile关键字
- 当变量是指针或者引用类型时,推导的结果会保留 const volatile关键字
int tmp = 250;
const auto a1 = tmp; //const int*
auto a2 = a1;
const auto &a3 = tmp;//const int*
auto &a4 = a3; //const int*
/*变量a1的数据类型为 const int,因此auto关键字被推导为 int类型
变量a2的数据类型为 int,但是a2没有声明为指针或引用因此 const属性被去掉, auto被推导为 int
变量a3的数据类型为 const int&,a3被声明为引用因此 const属性被保留,auto关键字被推导为 int类型
变量a4的数据类型为 const int&,a4被声明为引用因此 const属性被保留,auto关键字被推导为 const int类型*/
(二)auto的限制
- 不能作为函数的参数使用。因为只有在函数调用的时候才会给函数参数传递参数,auto要求必须给修饰的变量赋值,因此二者矛盾。

- 不能用于类的非静态成员变量的初始化(类的非静态成员是不属于类的是属于实例化对象的)

- 不能使用auto关键字定于数组

- 无法使用auto推导出模板参数

(三)auto的应用
- 用于STL容器的遍历
#include <iostream>
using namespace std;
#include <vector>
void test01(){
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
for(auto it=v.begin();it!=v.end();it++){
cout<<*it<<" ";
}
cout<<endl;
}
int main() {
test01();
system("pause");
return 0;
}

- 用于泛型编程
#include <iostream>
using namespace std;
class A1{
public:
static int get(){
return 100;
}
};
class A2{
public:
static string get(){
return "我被调用了";
}
};
template<typename T>
void function( ){
auto val =T::get();
cout<<"val:"<<val<<endl;
}
int main() {
function<A1>();
function<A2>();
system("pause");
return 0;
}

二、decltype
在不需要或者不能定义变量,但是希望的到某种类型,这时候可以适应decltype关键字,它的作用是在编译器编译时推导出一个表达式的类型
语法:decltype (表达式)
#include <iostream>
using namespace std;
void func(){
int a = 100;
decltype(a) b=99;
decltype(a+1.2) c=101.2;
decltype(a+b*c) d=1993.52;
cout<<"a:"<<a<<endl;
cout<<"b:"<<b<<endl;
cout<<"c:"<<c<<endl;
cout<<"d:"<<d<<endl;
}
int main(){
func();
system("pause");
return 0;
}

1446

被折叠的 条评论
为什么被折叠?



