We can bind a reference to an object only, not to a literal or to the result of a more genreal expression.
int i=42;
double d=1.2;
int &r=10;//error:initializer must be an object.
int &r1=d; //error: initializer must be an int object
While we can bind a reference to a constant to an object, a literal and to the result of a more general expression as shown-
int j=45;
double d=1.2;
const int &r2=j; // works
const int &r2=60; //works
const int &r3=d; //works
The reason mentioned in Primer is that the compiler creates a temporary object of type 'constant int' here.
const int temp=d;
const int &r3=temp;
But if this is the case, then this logic must apply to both the above cases, and both should work if the types of reference and the object are different. But in actual, if the types are different, then the program works only when the reference is of type 'reference to a constant' otherwise it shows an error. why ?