1.使用阿里云服务器,选择的centos版本,首先安装g++ 4.8.5
yum install gcc-c++
2.写个小程序测试一下:
#include "iostream" using namespace std; int main() { auto a = 1; return 0; }
编译报错,找不到auto这种类型
3.auto的限制
* auto不能用于函数参数
void testFunc(auto iParam) { } test.cpp:5:20: error: parameter declared ‘auto’ void testFunc(auto iParam) ^auto不能用于非静态成员变量
struct Foo { auto var1_ = 0; static const auto var2_ = 0; }; test.cpp:12:15: error: non-static data member declared ‘auto’ auto var1_ = 0; ^auto仅能用于推导static const的整型或者枚举成员。
4.什么时候用auto
* 优化代码美观性,减少冗余和繁琐的重复操作
int main() { auto a = 1; std::unordered_multimap无法提前感知函数返回类型的时候resultMap; //... std::pair<> ::iterator, std::unordered_multimap ::iterator> range = resultMap.equal_range(key); return 0; } => int main() { auto a = 1; std::unordered_multimap resultMap; //... auto range = resultMap.equal_range(key); return 0; }
class Foo { public: static int get(void) //... } class Bar { public: static const char* get(void) //... } templatevoid func() { auto val = A::get(); }