C++11新特性之auto的使用

所属分类: 软件编程 / C 语言 阅读数: 59
收藏 0 赞 0 分享

前言

C++是一种强类型语言,声明变量时必须明确指出其类型。但是,在实践中,优势我们很难推断出某个表达式的值的类型,尤其是随着模板类型的出现,要想弄明白某些复杂表达式的返回类型就变得更加困难。为了解决这个问题,C++11中引入的auto主要有两种用途:自动类型推断和返回值占位。auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除。前后两个标准的auto,完全是两个概念。

一、自动类型推断

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

#include <vector> 
#include <map> 
 
using namespace std; 
 
int main(int argc, char *argv[], char *env[]) 
{ 
// auto a;  // 错误,没有初始化表达式,无法推断出a的类型 
// auto int a = 10; // 错误,auto临时变量的语义在C++11中已不存在, 这是旧标准的用法。 
 
 // 1. 自动帮助推导类型 
 auto a = 10; 
 auto c = 'A'; 
 auto s("hello"); 
 
 // 2. 类型冗长 
 map<int, map<int,int> > map_; 
 map<int, map<int,int>>::const_iterator itr1 = map_.begin(); 
 const auto itr2 = map_.begin(); 
 auto ptr = []() 
 { 
 std::cout << "hello world" << std::endl; 
 }; 
 
 return 0; 
}; 
 
// 3. 使用模板技术时,如果某个变量的类型依赖于模板参数, 
// 不使用auto将很难确定变量的类型(使用auto后,将由编译器自动进行确定)。 
template <class T, class U> 
void Multiply(T t, U u) 
{ 
 auto v = t * u; 
} 

二、返回值占位

template <typename T1, typename T2> 
auto compose(T1 t1, T2 t2) -> decltype(t1 + t2) 
{ 
 return t1+t2; 
} 
auto v = compose(2, 3.14); // v's type is double 

三、使用注意事项

1、我们可以使用valatilepointer(*)reference(&)rvalue reference(&&) 来修饰auto

auto k = 5; 
auto* pK = new auto(k); 
auto** ppK = new auto(&k); 
const auto n = 6; 

2、用auto声明的变量必须初始化

auto m; // m should be intialized 

3、auto不能与其他类型组合连用

auto int p; // 这是旧auto的做法。 

4、函数和模板参数不能被声明为auto

void MyFunction(auto parameter){} // no auto as method argument 
 
template<auto T> // utter nonsense - not allowed 
void Fun(T t){} 

5、定义在堆上的变量,使用了auto的表达式必须被初始化

int* p = new auto(0); //fine 
int* pp = new auto(); // should be initialized 
 
auto x = new auto(); // Hmmm ... no intializer 
 
auto* y = new auto(9); // Fine. Here y is a int* 
auto z = new auto(9); //Fine. Here z is a int* (It is not just an int) 

6、以为auto是一个占位符,并不是一个他自己的类型,因此不能用于类型转换或其他一些操作,如sizeof和typeid

int value = 123; 
auto x2 = (auto)value; // no casting using auto 
 
auto x3 = static_cast<auto>(value); // same as above 

7、定义在一个auto序列的变量必须始终推导成同一类型

auto x1 = 5, x2 = 5.0, x3='r'; // This is too much....we cannot combine like this 

8、auto不能自动推导成CV-qualifiers(constant & volatile qualifiers),除非被声明为引用类型

const int i = 99; 
auto j = i; // j is int, rather than const int 
j = 100 // Fine. As j is not constant 
 
// Now let us try to have reference 
auto& k = i; // Now k is const int& 
k = 100; // Error. k is constant 
 
// Similarly with volatile qualifer 

9、auto会退化成指向数组的指针,除非被声明为引用

int a[9]; 
auto j = a; 
cout<<typeid(j).name()<<endl; // This will print int* 
 
auto& k = a; 
cout<<typeid(k).name()<<endl; // This will print int [9] 

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用C++能有一定的帮助,如果有疑问大家可以留言交流。

更多精彩内容其他人还在看

C++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多