Possible Duplicate:
typedef and containers of const pointers
Why is the code emitting an error?
int main()
{
//test code
typedef int& Ref_to_int;
const Ref_to_int ref = 10;
}
The error is:
error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘int’
I read the post on prolonging the lifetime of temporaries which says that temporaries can be bound to references to const. Then why is my code not getting compiled?
Here the type of
refis actuallyreference to intand notconst reference to int. The const qualifier is ignored.$8.3.2 says
const Ref_to_int ref;is equivalent toint& const ref;and notconst int& ref.