c++特性:auto
2016-02-01 12:40
851 查看
主要参考:【C++11】新特性——auto的使用
auto的作用是自动类型推断,可以简化编程的工作。
用auto声明的变量必须初始化
auto不能与其他类型组合连用
函数和模板参数不能被声明为auto
定义在堆上的变量,使用了auto的表达式必须被初始化
以为auto是一个占位符,并不是一个他自己的类型,因此不能用于类型转换或其他一些操作,如sizeof和typeid
定义在一个auto序列的变量必须始终推导成同一类型
auto不能自动推导成CV-qualifiers(constant & volatile qualifiers),除非被声明为引用类型
auto会退化成指向数组的指针,除非被声明为引用
下面是详细说明:
1、可以使用valatile,pointer(*),reference(&),rvalue reference(&&) 来修饰auto
2、函数和模板参数不能被声明为auto
3、 定义在堆上的变量,使用了auto的表达式必须被初始化
4、以为auto是一个占位符,并不是一个他自己的类型,因此不能用于类型转换或其他一些操作,如sizeof和typeid
5、 定义在一个auto序列的变量必须始终推导成同一类型
6、auto不能自动推导成CV-qualifiers(constant & volatile qualifiers),除非被声明为引用类型
7、auto会退化成指向数组的指针,除非被声明为引用
auto的作用是自动类型推断,可以简化编程的工作。
1、自动类型推导
int main() { // auto i; 错误,没有初始化 // auto int ii=10; 错误,旧用法,c++11不支持此用法 //1.自动帮助推导 类型 auto i =10; //int auto c = 'A'; //char auto str = "ssdg"; //const char* // 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.用于模板技术时的类型推断(这个nb) template<typename T, typename V> void Multi(T t, V v) { auto s = t*v; }
2、返回值占位
template<typename T, typename V> auto add(T t, V v) -> decltype(t+v) { return t+v; }//decltype 稍后讲解
3、注意事项
可以使用valatile,pointer(*),reference(&),rvalue reference(&&) 来修饰auto用auto声明的变量必须初始化
auto不能与其他类型组合连用
函数和模板参数不能被声明为auto
定义在堆上的变量,使用了auto的表达式必须被初始化
以为auto是一个占位符,并不是一个他自己的类型,因此不能用于类型转换或其他一些操作,如sizeof和typeid
定义在一个auto序列的变量必须始终推导成同一类型
auto不能自动推导成CV-qualifiers(constant & volatile qualifiers),除非被声明为引用类型
auto会退化成指向数组的指针,除非被声明为引用
下面是详细说明:
1、可以使用valatile,pointer(*),reference(&),rvalue reference(&&) 来修饰auto
auto k = 5; auto kkk = new auto(k);// kkk is int* auto* pK = new auto(k); //pK is int* auto** ppK = new auto(&k);//ppK is int**
2、函数和模板参数不能被声明为auto
template<auto T> //not allow ……
3、 定义在堆上的变量,使用了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)
4、以为auto是一个占位符,并不是一个他自己的类型,因此不能用于类型转换或其他一些操作,如sizeof和typeid
int value = 123; auto x2 = (auto)value; // no casting using auto auto x3 = static_cast<auto>(value); // same as above
5、 定义在一个auto序列的变量必须始终推导成同一类型
auto x1 = 5, x2 = 5.0, x3='r'; // This is too much....we cannot combine like this
6、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
7、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++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性