Does casting a variable to another type return a temporary copy of that variable? if so then why can’t you reference the temporary variable to a function.
void func(int &i) //error converting parameter 1 from int to int&
{
}
int main()
{
double d = 6.8;
func(int(d));
}
Yes casting returns an rvalue (temporary value), but a mutable reference needs an lvalue.
Try this instead:
If
funcis not going to modifyi, the input argument should be a const reference, which can accept an rvalue.(but for primitives
func(int i)is going to be more efficient.)