Cannot initialize non-const reference from convertible type
Posted
by Julien L.
on Stack Overflow
See other posts from Stack Overflow
or by Julien L.
Published on 2010-03-25T15:05:02Z
Indexed on
2010/03/25
15:13 UTC
Read the original article
Hit count: 249
Hi,
I cannot initialize a non-const reference to type T1 from a convertible type T2. However, I can with a const reference.
long l;
const long long &const_ref = l; // fine
long long &ref = l; // error: invalid initialization of reference of
// type 'long long int&' from expression of type
// 'long int'
Most problems I encountered were related to r-values that cannot be assigned to a non-const reference. This is not the case here -- can someone explain? Thanks.
© Stack Overflow or respective owner