mercredi 30 septembre 2015

C++ : Different deduction of type auto between const int * and cont int &

Here are the code samples.

a. int ii = 0;
b. const int ci = ii;
c. auto e = &ci; --> e is const int *
d. auto &f = 42; --> invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
e. const auto &g = 42 --> ok

Observation:
1. for clause c) the type const is automatically deduced
2. for clause d) the type const is not automatically deduced
3. for clause e) the type const has to be added manually in order for it to work.

Why type const is automatically deduced for clause c but not d?

Aucun commentaire:

Enregistrer un commentaire