Here is an exercise from C++ Primer 5th Edition:
Exercise 16.45: Given the following template, explain what happens if we call g on a literal value such as 42. What if we call g on a variable of type int? P.690
template <typename T>
void g(T&& val)
{
std::vector<T> v;
}
int main()
{
//g(42);
int i;
g(i);
}
When calling on 42
, it compiled.
When on i
, the compiler complained a lot of errors, part of which is pasted as below.
forming pointer to reference type 'int&'
My questions are
When calling on literal value ,
42
in this case, what type was deduced forT
?when on
i
, why didn't it compile? How to understand these error messages?