Doubt about C ++ interview question

I read Answers to interview questions in C ++ , among which there is a question that puzzles me:

Q: When are temporary variables created by the C ++ compiler?

A: if the function parameter is a “constant reference”, the compiler generates a temporary variable in the following two ways.

a) The actual argument is the correct type, but it is not an Lvalue

double Cube(const double & num) { num = num * num * num; return num; } double temp = 2.0; double value = cube(3.0 + temp); // argument is a expression and not a Lvalue 

b) The actual argument is of the wrong type, but a type that can be converted to the correct type

  long temp = 3L; double value = cuberoot(temp); // long to double conversion 

My question is when the function argument is a const reference , why does the compiler generate a temporary variable, is it not contradictory? Also, if the Cube function does not compile because it modifies the const argument?

+4
source share
7 answers

You are allowed to pass the results of an expression (including implicit casting) to a const-reference. The rationale is that although (const X & value) may be cheaper to use, depending on the cost of copying type X than (X value) , the effect is almost the same; value used, but not modified (prohibition of any risky const-casting). Therefore, it is harmless to allow the creation and transfer of a temporary function object.

You are not allowed to do this with a pointer to a constant or a reference to non-const, because unforeseen (and bad) things may occur, for example, you can expect that a long tempo will be dropped to a long one, which will not happen.

You are wrong with respect to num = num * num * num; . This is a mistake in the text, but the argument made by him takes place.

+1
source

I do not see anything contradictory here. If the argument is not an lvalue or an incorrect type, the link cannot be bound directly to the argument for obvious reasons; therefore, an intermediate temporary type is required. Link refers to this temporary location.

The Cube function is really broken (poorly formed), as it tries to change the value of const .

+7
source

Looks like me - and gcc generates an error:

 const_ref.cpp: In function 'double cube(const double&)': const_ref.cpp:3: error: assignment of read-only reference 'num' 
+3
source

The compiler can generate a temporary variable. It's not obligatory.

And yes, Cube should not compile.

+2
source

Because in both examples there is no non-temporal object of the correct type.

0
source

I believe that you are correctly parsing a function cube. In any case, this should fail, and this happens on my compiler (VC ++ 2008).

Regarding the creation of a temporary:

A temporary value to return a const reference will be created whenever the actual argument:

i) is not the correct type for reference, and ii) can be implicitly converted to the correct type.

In example a), a temporary double is created from your question to store the value 3.0 + temp . Then Cube() is called with a const reference to a temporary one. This is because you cannot refer to 3.0 + temp because it is not a variable (this is rvalue - the result of an expression), and therefore it does not have a memory address and cannot return the link. Implicitly, the compiler will create a temporary double , and then assign it a value of 3.0 + temp .

In your example b) you have long , but your function requires a double . The compiler will implicitly convert a long to double . This is done by creating a temporary double , assigning the converted temp value to it, and then creating a const link for the temporary one and passing that link to cuberoot

0
source

Yes. Cube (), as you showed here, should not compile.

0
source

All Articles