C++11:类型推导auto
来源:互联网 发布:蛆娘黑历史 知乎 编辑:程序博客网 时间:2024/05/16 11:44
原文:http://blog.csdn.net/huang_xw/article/details/8760403
C++11中引入的auto主要有两种用途:自动类型推断和返回值占位。auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除。前后两个标准的auto,完全是两个概念。
1. 自动类型推断
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;
- }
2. 返回值占位
- 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
3.使用注意事项
①我们可以使用valatile,pointer(*),reference(&),rvalue reference(&&) 来修饰auto- auto k = 5;
- auto* pK = new auto(k);
- auto** ppK = new auto(&k);
- const auto n = 6;
- auto m; // m should be intialized
- auto int p; // 这是旧auto的做法。
- void MyFunction(auto parameter){} // no auto as method argument
- template<auto T> // utter nonsense - not allowed
- void Fun(T t){}
- 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)
- int value = 123;
- auto x2 = (auto)value; // no casting using auto
- auto x3 = static_cast<auto>(value); // same as above
- auto x1 = 5, x2 = 5.0, x3='r'; // This is too much....we cannot combine like this
- 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
- 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]
0 0
- C++11:类型推导auto
- [C++]高效使用c++11--理解auto类型推导
- Modern C++(一)auto自动类型推导
- C++11 auto自动类型推导
- C++11之auto类型推导
- auto关键字---------------类型推导
- C++ auto类型推导
- 《Effective Modern C++》读书笔记(2) -- auto类型推导(auto type deduction)
- [C++11/14] 自动类型推导——auto
- C++ 11 学习1:类型自动推导 auto和decltype
- C++11初探:类型推导,auto和decltype
- item 2:理解auto类型的推导
- C++以auto完成类型自动推导
- 《Effective Modern C++》翻译--条款2: 理解auto自动类型推导
- [C++] auto类型说明符
- C++11系列-改进的类型推导:auto、decltype和新的函数语法
- C++11系列-改进的类型推导:auto、decltype和新的函数语法
- C++11系列-改进的类型推导:auto、decltype和新的函数语法
- 利用TextArea 定义不同的显示颜色或者显示格式
- N!的素因子分解
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock
- CSS高效开发实战:CSS 3、LESS、SASS、Bootstrap、Foundation --读书笔记(5)使用放射渐变制作光影效果
- 设置android:supportsRtl="true"无效问题
- C++11:类型推导auto
- java实体类实现序列化的意义
- 特种浓缩分离:弧形阳极电泳工艺流程
- Android Java 开发工具集合
- 动态规划:从新手到专家
- nyoj78圈水池(凸包问题)
- LeetCode Maximum Gap
- UITableView 优化
- Jsystem test case 的rerun实现