Is it safe to bind an unsigned int to a signed int reference?
References can't bind to objects with different type directly. Given const int& s = u;
, u
is implicitly converted to int
firstly, which is a temporary and then s
binds to the temporary int
. (Lvalue-references to const
(and rvalue-references) could bind to temporaries.) The lifetime of the temporary is prolonged to the lifetime of s
, i.e. it'll be destroyed when get out of main
.