When we use auto
to deduce type of an object, high level const
s are ignored, for e.g.
const int i = 0; //i is high level const
auto j = i; //j is an int
However, low level const
s are never ignored, e.g.
const int * ptr = nullptr //ptr is low level const;
auto ptr2 = ptr; //ptr2 is also const int*
But when I define a low level const
reference, and use auto
, it loses the const
int var;
const int &ref = var; //ref is low level const reference
auto ref2 = ref; //ref2 is int&!`
Why is this behavior?
Tried on VS2017 C++ compiler
New contributor
Madhusudan Mudhol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.