Possible Duplicate:
auto reference in c++11
The more I learn C++, the more I have come to realize that so far (almost; see below) everything in it basically just makes sense. I find that I don’t really have to learn any rules by heart because everything behaves as expected. So the main thing becomes to actually understand the concepts, and then the rest takes care of itself.
For instance:
const int ci = 0;
auto &a = ci; //automatically made const (const int &)
This works and makes sense. Anything else for the type of a would just be absurd.
But take these now:
auto &b = 42; //error -- does not automatically become const (const int)
const auto &c = 42; //fine, but we have to manually type const
Why is the first an error? Why doesn’t the compiler automatically detect this? Why does the const have to be typed out manually? I want to really understand why, on a fundamental level so that things make sense, without having to learn any rigid rules by heart (see above).
The type of
42isint, notconst int.