Why non-template function does not compile where as template function compiles?
-
I am trying to call two identical functions (one is templated and the other one is normal). The templated one compiles successfully, but the non-templated one gives compilation error. Why?
template
void Template_universal_ref(ParamType&& param)
{
cout << "Template_universal_ref, param : " << param << endl;
}void NonTemplate_universal_ref(int&& param)
{
cout << "NonTemplate_universal_ref, param : " << param << endl;
}int main()
{
int x=5;
Template_universal_ref(x);
//NonTemplate_universal_ref(x); // This is giving compilation error
return 0
}In my view
NonTemplate_universal_ref(x);
is special version of
Template_universal_ref(x);
. Why does NonTemplate_universal_ref(x); result in compilation error?
-
I am trying to call two identical functions (one is templated and the other one is normal). The templated one compiles successfully, but the non-templated one gives compilation error. Why?
template
void Template_universal_ref(ParamType&& param)
{
cout << "Template_universal_ref, param : " << param << endl;
}void NonTemplate_universal_ref(int&& param)
{
cout << "NonTemplate_universal_ref, param : " << param << endl;
}int main()
{
int x=5;
Template_universal_ref(x);
//NonTemplate_universal_ref(x); // This is giving compilation error
return 0
}In my view
NonTemplate_universal_ref(x);
is special version of
Template_universal_ref(x);
. Why does NonTemplate_universal_ref(x); result in compilation error?
-
I am trying to call two identical functions (one is templated and the other one is normal). The templated one compiles successfully, but the non-templated one gives compilation error. Why?
template
void Template_universal_ref(ParamType&& param)
{
cout << "Template_universal_ref, param : " << param << endl;
}void NonTemplate_universal_ref(int&& param)
{
cout << "NonTemplate_universal_ref, param : " << param << endl;
}int main()
{
int x=5;
Template_universal_ref(x);
//NonTemplate_universal_ref(x); // This is giving compilation error
return 0
}In my view
NonTemplate_universal_ref(x);
is special version of
Template_universal_ref(x);
. Why does NonTemplate_universal_ref(x); result in compilation error?
int&&
is a rvalue reference. Until C++11 we only had lvalue references. InNontemplate_universal_ref(x)
, x is a lvalue, not an rvalue. Try the following:template void Template_universal_ref(ParamType&& param)
{
cout << "Template_universal_ref, param : " << param << endl;
}void NonTemplate_universal_ref(int&& param)
{
cout << "NonTemplate_universal_ref(int&&), param : " << param << endl;
}void NonTemplate_universal_ref(int& param)
{
cout << "NonTemplate_universal_ref(int&), param : " << param << endl;
}int main()
{
int x=5;
Template_universal_ref(x);
NonTemplate_universal_ref(x);
NonTemplate_universal_ref(x+3);
return 0;
}Note that in the second call to NonTemplate_universal_ref, the argument x+3 is not an lvalue, that is, it cannot be assigned to. Now, maybe someone can explain why the call via the template works?