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;>

rather than you thinkMyfunctemp

CodePudding user response:

reference 1st floor SleekStone 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


I know to be named after t1 on behalf of lvalue, that can be whole (& amp; & T1) as a parameter f function to receive the right value?
While others, such as void func (int a) {cout & lt; CodePudding user response:

To understand the code, it is estimated that you will understand
Int& & R1=10;//right
Int& & R2=r1;//error
When you use an rvalue references can only be bound to a temporary value, rather than the binding of an existing value,
Give you a code
 # include & lt; Iostream> 
Using namespace STD.
Void RunCode (int& & M) {
Cout & lt; <"The right call" & lt; }
Void RunCode (int& M) {
M++;
Cout & lt; <"Left call" & lt; }


Int main ()
{
Int& & B=10;
RunCode (10);//call the RunCode (int& & amp;);
RunCode (b);//call the RunCode (int&);
return 0;
}

CodePudding user response:

To understand the c + + reference was clear to fold

CodePudding user response:

There is also a common function accepts the right value, you understand there is deviation, the so-called reference is introduced in order to reduce the number of copies, as a function of the parameter is a reference (or so), it will direct the argument itself is passed to a function (usually by pointer), and your example, the parameter is a value type (int), whether you pass the left or the right values occur to copy, that is to say, inside the function, the parameter changes will not affect the caller,

CodePudding user response:

What is the relationship between the reference folding, reference folding is not within the system implementation,

CodePudding user response:

Perfect forward is to use a reference to the folding rules

CodePudding user response:

refer to 6th floor truth is right or wrong response:
to understand the c + + reference was clear to fold


Void func (int a) {coutIn the main function func function called: (I)=100; Func (I);//set up
Func (88);//set up
The code above with reference fold isn't think of it this way: when introduced into the argument I for argument I left value, so a can directly receive,
When introduced to right value of 88, the actual is 88 system to infer func function prototype void func (int int& & amp;) On the right side of the int& & To receive, and (int int& & amp;) Front for lvalue, behind is the right value, by folding rules: have left value results for lvalue, namely whether (int int& & amp;) As a whole is an lvalue?

CodePudding user response:

Fun (int a) this is not a reference, whether you pass the left or the right values, are copied to a first and then operations, has nothing to do with your problem

CodePudding user response:

refer to fifth floor truth is right or wrong response:
understand this code, you will understand
Int& & R1=10;//right
Int& & R2=r1;//error
When you use an rvalue references can only be bound to a temporary value, rather than the binding of an existing value,
Give you a code
 # include & lt; Iostream> 
Using namespace STD.
Void RunCode (int& & M) {
Cout & lt; <"The right call" & lt; }
Void RunCode (int& M) {
M++;
Cout & lt;
  • Related