auto i = 0, *p = &i; // okauto sz = 0, pi = 3.14; // errorauto p2 = &i; // ok
Exception for Deducing Compound Types and const
auto ordinarily ignores top-level consts, and low-level const is kept. If low-level is ignored, we can change the underlain.
int i = 0;const int ci = i;auto &g = ci; // ok: g is const int& that is bound to ciauto *p = &ci; // ok: p is pointer to const intauto &h = 42; // error: const auto &j = 42; // ok
Why the const is not ignored? 这其实是 compound type 的特性,不带个 const 就能改 underlain 了
int i = 42, *p = &i, &r = i;decltype(r + 0) b; // ok: addition yields an int; b is an (uninitialized) int decltype(*p) c; // error: c is int& and must be initialized decltype((i)) d; // error: d is int& and must be initializeddecltype(i) e; // ok: e is an (uninitialized) int