The quotations below are all from the book: C++ Primer 5th ed."
quote 1
autoordinarily ignores top-level consts. As usually in initialization. low-level const, such as when an initalizer is a pointer to const, are kept. ( -- pp.69)
quote 2
When the expression to which we apply
decltypeis a variable,decltypereturns the type of that variable, including top-level const and references (-- pp. 70).
Compare with decltype, why auto drops the top-level const in type deducing?, then I found:
autotells the compiler to deduce the type from the initializer, But implication, a variable that usesautoas its type specifier must have an initializer (-- pp. 69)
My Understanding so far is: auto infers the type from the initializer. The initilizer is rvalue type which is not saved in the stack, therefore, it has no top-level const at all. Because of this, auto drops the top-level const during type deducing.
I don't know if my understanding is correct or wrong, please correct it if it is wrong.
Aucun commentaire:
Enregistrer un commentaire