update Aug 4, 2019
auto 使用和templates相同的type deduction mechanism, 不过对于brace-init lists, auto 会将其 deduce 为 std::initializer_list.
auto
std::initializer_list
auto x = expression;
去掉reference和cv qualifiers,然后match type。例如对于 const int& f(){...},那么 auto x = f() 会 deduce x 为 int,而不是 const int&;
const int& f(){...}
auto x = f()
x
int
const int&
auto& x = expression;
const auto&
保留cv qualifiers,因此对于之前那个f(), auto& x = f() 会 deduce 为 const int&, 而 const auto& 只是在这个基础上强制添加const;
f()
auto& x = f()
auto&& x = expression;
使用 reference-collapsing rules, 和template code 中的 forwarding references 类似。如果x是lvalue,则deduce lvalue reference, 否则deduce rvalue reference.
使用 auto 如果需要一个 copy,否则用 auto& 或者 const auto& 依情况而定。
auto&
Last updated 6 years ago