C++11学习笔记(1) —— auto

C++11 的到来又为C++注入了新鲜的血液,更严格的类型检查,新的功能特性以及新收录的STL,都使得C++11更为安全和高效。笔者将C++11的学习过程记录下来,不对之处还望指正。

IDE: vs2012


1.简介

auto是C++早就具备的关键字(局部变量默认定义为auto类型),但是C++11中,auto被赋予了新的含义 —— 定义任意类型


2.根据初始化推导类型

auto 可以根据初始化来自动推导类型,例如:

#include "stdafx.h"
#include<iostream>
using namespace std;

class TestClass
{
public:
	TestClass():a(0),b(0){}

	int a;
	int b;
};

int _tmain(int argc, _TCHAR* argv[])
{
	auto i = 2;
	auto j = 3.4f;
	auto k = 4.0;

	TestClass tc;
	auto obj = tc;

	cout<<"Type of i : " << typeid(i).name() << endl;
	cout<<"Type of j : " << typeid(j).name() << endl;
	cout<<"Type of k : " << typeid(k).name() << endl;
	cout<<"Type of obj : " << typeid(obj).name() << endl;

	return 0;
}


输出结果如下图所示:

可以看出,auto 关键字定义的变量,编译器会根据初始化操作自动确定其类型。注意,对于浮点型,如果不以 f 标记结尾则默认为double数据类型。这种自动类型的推导,可以简化代码,例如,在STL Container的使用过程中,经常需要定义 iterator 来对容器的数据进行操作,如下所示:

vector< list< SomeType> >  testVec;
vector< list< SomeType> >::iterator iter = testVec.begin();


通过使用 auto 可以简化为

auto iter = testVec.begin();


3. 其它限定词 (const , *, & 等 )

auto 还可以和其它的限定词一起使用,例如 const ,指针, 取址等,如下例所示

#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	const auto n1 = 1;
	auto const n2 = 2;
	auto n3 = 3;
	const int n4 = 4;
	auto p1 = new int(4);
	auto* p2 = new int(5);

	//compile error!!
	//n1 = 11; 

	//compile error!!
	//n2 = 22;

	cout<<"Type of n1 : " << typeid( n1 ).name() << endl;
	cout<<"Type of n2 : " << typeid( n2 ).name() << endl;
	cout<<"Type of n3 : " << typeid( n3 ).name() << endl;
	cout<<"Type of n4 : " << typeid( n4 ).name() << endl;
	cout<<"Type of p1 : " << typeid( p1 ).name() << endl;
	cout<<"Type of p2 : " << typeid( p2 ).name() << endl;

	return 0;
}

输出结果如下图所示:


由输出结果可以看出,使用const对auto类型变量限定后, 该变量即为常量类型,不可修改(对于const类型,输出类型没有const)。同时也可以看出,使用指针对auto进行限定和不使用指针,变量的类型都相同,例如 p1 和 p2 , 类型由编译器自动推导。


对于const类型赋值,auto 并不会自动推导出const限定,必须使用引用,例如

const int i=1;
auto j = i;  // j is int
auto& k = i; // k is const int


4. 解决特殊问题

在使用模板进行泛型编程时,有些时候类型难以确定,这时 auto 关键字可以发挥重要作用(配合 decltype,后面讲述 ),例如

#include<iostream>
using namespace std;

template< class T1, class T2 >
auto Fun( T1 t1, T2 t2 ) -> decltype( t1*t2 )
{
	return t1 * t2;
}

int _tmain(int argc, _TCHAR* argv[])
{
	auto nRes = Fun( 2, 3 );
	auto fRes = Fun( 2.0f, 4 );
	auto dRes = Fun( 2.0, 5 );

	cout<<"Type of nRes : " << typeid( nRes ).name() << endl;
	cout<<"Type of fRes : " << typeid( fRes ).name() << endl;
	cout<<"Type of dRes : " << typeid( dRes ).name() << endl;

	return 0;
}

其输出结果如下图所示


对于函数 Fun, 两个不同的参数类型会导致最终的结果类型不确定,如果通过函数重载来实现该功能,会书写大量的代码,通过模板, auto , 和 decltype 可以轻松实现对类型的推导,将类型的决定权交给编译器!


5. 注意事项

auto 的使用非常方便,但也并非任何场合都使用,需要遵守一定的规则

#include<iostream>
using namespace std;

//1. Compile Error: error C3533: 'auto': a parameter cannot have a type that contains 'auto'
void fun(auto i)
{
	return;
}

//2. Compile Error: error C3533: 'auto': a parameter cannot have a type that contains 'auto
template< auto T >
void fun2() { return ;}

int _tmain(int argc, _TCHAR* argv[])
{
	//3. Compile Error: error C3531: 'i': a symbol whose type contains 'auto' must have an initializer.
	auto i;

	//4. Compile Error: error C3530: 'auto' cannot be combined with any other type-specifier
	auto double d = 2.9;

	//5. Compile Error: error C3537: 'auto': you cannot cast to a type that contains 'auto'
	int j = 8;
	auto jj = (auto)j;

	return 0;
}

(1)auto 不能作为函数参数

(2)auto 不能作为模板类型参数

(3)auto 定义的变量必须进行初始化

(4)auto 不能与其它类型一起使用

(5)不能使用auto进行类型转换

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值