lundi 22 janvier 2018

Why auto type specifier ignores the top-level const?

The quotations below are all from the book: C++ Primer 5th ed."

quote 1

auto ordinarily 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 decltype is a variable, decltype returns 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:

auto tells the compiler to deduce the type from the initializer, But implication, a variable that uses auto as 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