Consider this:
void test(int&& param){/*...*/}
int main(){
int a{444};
test(a);
}
Certainly It'll not compile because there's no conversion is defined for int
to int&&
. But the following code:
template<typename T>
void test(T&& param){/*..*/}
int main(){
int a=826;
test(a);
return 0;
}
will compile. But why? What's the reason? There's nothing special about the later except that function test will instantiates for all types T
. But we still need to pass rvalue as parameter. So why does it,the later, compile? - without any complaint. Then I tried the following:
template<typename T>
void test(T&& param){/*..*/}
template<typename T>
void test(T param){/*...*/}
int main(){
int a=826;
test(a);
return 0;
}
The compiler complains because function call is ambiguous on whether calling test(T&& param)
with T=int&
or test(T param)
with T=int
.
Now I am totally baffled on what to deduce. What special does the Template do in here? Why doesn't it conform with the rules on rvalues and lvalues?
Thanks in Advance