【c++11】 auto

C++11中引入的auto主要有两种用途:自动类型推断和返回值占位。auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除。前后两个标准的auto,完全是两个概念。

1. 自动类型推断

    auto自动类型推断,用于从初始化表达式中推断出变量的数据类型。通过auto的自动类型推断,可以大大简化我们的编程工作。下面是一些使用auto的例子。
#include <iostream>
#include <vector>  
#include <map>
#include <cassert>
#include <typeinfo>

using namespace std;  

template <class T, class U>  
auto Multiply(T t, U u) -> decltype(t*u);   

int main()  
{  
	//  auto a;                 // 错误,没有初始化表达式,无法推断出a的类型  
	//  auto int a = 10;        // 错误,auto临时变量的语义在C++11中已不存在, 这是旧标准的用法。  

	// 1. 自动帮助推导类型  
	auto a = 10;  
	auto c = 'A'; 
	auto s("hello");
	// 可以使用&、*、const修饰
	const auto& irs = a;
	const auto* cip = &a;
	auto* const icp = &a;
	const auto* const cicp = &a;
//	auto str[] = "hello"; // error

	int i = 12;
// 	icp = &i;   // error, 不能给常量赋值
// 	*cicp = 15; // error, 不能给常量赋值
	cout << typeid(c).name() << endl;		// char
	cout << typeid(s).name() << endl;		// char const*
	cout << typeid(irs).name() << endl;		// int, 但irs: const int&	
	cout << typeid(icp).name() << endl;		// int *, 但icp: int* const 
	cout << typeid(cip).name() << endl;		// int const* == const int*

//	assert(typeid(s) == typeid(char const*));

	// 2. 类型冗长  
	map<int, map<int,int> > map_;  
	map<int, map<int,int>>::const_iterator itr1 = map_.begin();  
	const auto itr2 = map_.begin();  
	auto ptr = []()  
	{  
		cout << "hello world" << endl;  
	};  
//	cout << typeid(ptr).name() << endl; // class <lambda_...>
	ptr(); // 函数调用

	auto mutil = Multiply(2, 3); // 模板函数实例化
	assert(typeid(mutil) == typeid(int));

	return 0;  
}; 

2. 返回值占位

// 3. 使用模板技术时,如果某个变量的类型依赖于模板参数,  
// 不使用auto将很难确定变量的类型(使用auto后,将由编译器自动进行确定)。  
// 返回值占位:同时使用auto、decltype
template <class T, class U>  
auto Multiply(T t, U u) -> decltype(t*u)   
{  
	auto v = t * u; 
	// change v...
	return v;  
//	return t * u;
}  


3.使用注意事项

①我们可以使用valatile,pointer(*),reference(&),rvalue reference(&&) 来修饰auto
  1. auto k = 5;  
  2. auto* pK = new auto(k);  
  3. auto** ppK = new auto(&k);  
  4. const auto n = 6;  
②用auto声明的变量必须初始化
  1. auto m; // m should be intialized    
③auto不能与其他类型组合连用
  1. auto int p; // 这是旧auto的做法。  
④函数和模板参数不能被声明为auto
  1. void MyFunction(auto parameter){} // no auto as method argument  
  2.   
  3. template<auto T> // utter nonsense - not allowed  
  4. void Fun(T t){}  
⑤定义在堆上的变量,使用了auto的表达式必须被初始化
  1. int* p = new auto(0); //fine  
  2. int* pp = new auto(); // should be initialized  
  3.    
  4. auto x = new auto(); // Hmmm ... no intializer  
  5.      
  6. auto* y = new auto(9); // Fine. Here y is a int*  
  7. auto z = new auto(9); //Fine. Here z is a int* (It is not just an int)  
⑥以为auto是一个占位符,并不是一个他自己的类型,因此不能用于类型转换或其他一些操作,如sizeof和typeid
  1. int value = 123;  
  2. auto x2 = (auto)value; // no casting using auto  
  3.   
  4. auto x3 = static_cast<auto>(value); // same as above   
⑦定义在一个auto序列的变量必须始终推导成同一类型
  1. auto x1 = 5, x2 = 5.0, x3='r';  // This is too much....we cannot combine like this  
⑧auto不能自动推导成CV-qualifiers(constant & volatile qualifiers),除非被声明为引用类型
  1. const int i = 99;  
  2. auto j = i;       // j is int, rather than const int  
  3. j = 100           // Fine. As j is not constant  
  4.   
  5. // Now let us try to have reference  
  6. auto& k = i;      // Now k is const int&  
  7. k = 100;          // Error. k is constant  
  8.   
  9. // Similarly with volatile qualifer  
⑨auto会退化成指向数组的指针,除非被声明为引用
  1. int a[9];  
  2. auto j = a;  
  3. cout<<typeid(j).name()<<endl; // This will print int*  
  4.   
  5. auto& k = a;  
  6. cout<<typeid(k).name()<<endl; // This will print int [9] 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值