C++ const reference temporary variable reference parameter details

  • 2020-05-12 02:55:51
  • OfStack

C++ references - temporary variables, reference parameters, and const references

If the real participant reference parameters do not match, C++ will generate temporary variables. If the reference parameter is const, the compiler generates temporary variables in two cases:

The argument type is correct, but not lvalue

Arguments are not of the correct type, but can be converted to the correct type

Lvalue parameters are data objects that can be referenced; for example, variables, array elements, structural members, references, and dereferenced Pointers are lvalues, and non-lvalues include literal constants and expressions containing polynomials. Define 1 function


Double refcube(const double& ra)

{
     Returnra*ra*ra;
}


double side = 3.0;

double* pd = &side;

double& rd = side;

long edge = 5L;

double lens[4]={2.3,3.4,4.5,6.7};

double c1 = refcube(side); // ra  is side

double c2 = refcube(lens[2]); // ra is lens[2]

double c3 = refcube(rd);  // ra  is  rd

double c4 = refcube(*pd); // ra  is *pd

double c5 = refcube(edge); // ra  It's a temporary variable 

double c6 = refcube(7.0); // ra  It's a temporary variable 

double c7 = refcube(side+10.0); // ra  It's a temporary variable 

The parameters side lens[2] rd and *pd are named data objects of type double, so you can create references to them without the need for temporary variables. But while edge is a variable, it is of an incorrect type, and an double reference cannot point to long. On the other hand, both parameters 7.0 and side+10.0 are of the correct type, but have no names, in which case the compiler will generate a temporary anonymous variable and have ra point to it. These temporary variables exist only for the duration of the function call and can be removed at will by the server compiler

So why is this behavior possible for constant references, but not otherwise?


Void swapr(int& a,int& b)

{

     Inttemp;

     Temp=a;

     A= b;

     B= temp;

}

Under the earlier C++ looser rules, what would happen if you did the following?

Long a = 3,b = 5;

Swapr(a,b);

The types here don't match, so the compiler will create two temporary int variables, initialize them as 3 and 5, and then swap the contents of the temporary variables, while a and b remain the same

In short, if the purpose of a function that accepts a reference parameter is to modify a variable that is passed as a parameter, creating a temporary variable will prevent that from happening. The solution is to prohibit the creation of temporary variables.

Now let's look at the refcube() function, which is only intended to use the passed values, not to modify them, so temporary variables do no harm. Instead, it makes the function more generic in terms of the types of arguments it can handle. Therefore, if the declaration specifies the reference as const, C++ will generate temporary variables if necessary. In fact, for the C++ function whose formal parameter is const reference, if the arguments do not match, it will behave like passing by value. To ensure that the original data is not modified, a temporary variable will be used to store the value.

(PS: if the function call parameter is not lvalue or does not match the type of the corresponding const reference parameter, C++ creates an anonymous variable of the correct type, passes the value of the function call parameter to the anonymous variable, and lets the parameter reference the variable.)

const should be used whenever possible

Use cosnt to avoid programming errors that inadvertently modify data all the time

Using const enables the function to handle const and non-const arguments, otherwise it will only accept non-const data

Using the const reference enables the function to correctly generate and use temporary variables


Related articles: