auto

auto

1.0 定义变量时,必须初始化
auto a = 10;   // 正确
auto b;        // 错误
1.1 不能用于函数参数
void func(auto a = 1);  // 错误
1.2 不能用于函数参数
struct Foo
{
    auto var1_ = 0;  // error: auto不能用于非静态成员变量
    static const auto var2_ = 0; // 正确
};
1.3 无法定义数组
int arr[10] = {0};
auto aa = arr;  // OK: aa -> int *
auto rr[10] = arr;  // 错误,auto无法定义数组
1.4 模板实例化类型不能是auto类型
vector<auto> a = { 1 };  // 错误,有初始化也不行
1.5 无法推导出模板参数
struct Bar {};
Bar<int> bar;
Bar<auto> bb = bar;  // 错误:auto无法推导出模板参数
1.6 auto用于占位符,真正的返回值在后面定义
template<class Lhs, class Rhs>
auto adding_func(const Lhs &lhs, const Rhs &rhs) -> decltype(lhs + rhs)
{
    return lhs + rhs;
}
cout << adding_func<double, int>(10, 2) << endl;
1.7 支持区间迭代
for (auto &x : my_array) {}
posted @ 2019-06-24 14:52  osbreak  阅读(260)  评论(0编辑  收藏  举报