Home > Back-end > Template function parameters can't receive the right value, why the ordinary function can direc
Template function parameters can't receive the right value, why the ordinary function can direc
Time:11-17
TemplateVoid myfunctemp (F F, T1 & amp; & T1, T2 & amp; & T2)//class template function, universal reference parameter as a template function, can receive the left value, and can receive the right values, { F (t1, t2); } Void myfunc (int & amp; & V1, int & amp; V2) { Cout & lt; Cout & lt; } Void func (int a) {cout & lt; Int main () { Int I=8;//the left value Myfunctemp (myfunc, 30, I);//right 30 to universal reference value parameter t1, but function f function parameters in the body t1 to lvalue, compiled by?? 1) why not here Func (I);//normal function func parameter a is left value, it can receive argument I (l); Func (99);//normal function func parameter a is left value, but can receive 99 (right) argument; ?? (2) return 0; } The above marked?? And???? (2) the place more doubt, because (1) using a function template parameter 30 and I forwarded to function f, t1 (left) cannot receive rvalue (30), the compiler cannot pass, the compiler error right value can not be bound to the left value, but in the ordinary function (2) why can directly to the right value 99 to lvalue parameters a? Pray god to light? Here is to compile the results
CodePudding user response:
templateTemplateVoid myfunctemp (F F, T1 & amp; & T1, T2 & amp; & T2)//class template function, universal reference parameter as a template function, can receive the left value, and can receive the right values, { F (STD: : forward (t1), STD: : forward (t2)); }
T1 and t2 in myfunctemp function to be named the f, degenerated into an lvalue, should use the STD: : forward forward
CodePudding user response:
It should be understood,
#include
Using namespace STD.
Void func0 (int a) { Cout & lt; }//normal function
Void func1 (int & amp; A) { Cout & lt; }//normal function
Void func2 (const int & amp; A) { Cout & lt; }//normal function
Int main () { Int I=8;//the left value Func0 (I); Func0 (99);
Func1 (I); Func1 (99);
Func2 (I); Func2 (99); return 0; }
CodePudding user response:
Because myfunctemp (myfunc, 30, I); When specialized prototype is Myfunctemp int , int & amp;>