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
source share