If a local object is returned in a function call, it has to do at least three steps:
- Copy constructor is called to hold a copy.
- Destroy local object.
- A copy is return.
For example:
x = y + z
If x is an integer object. A copy of y + z should be returned, then a new object is created, then assignment operator of x will take this object as parameter.
So my questions are:
- Is the same process used for built-in type such as
int,double…? - If they’re not the same, how’s it done?
The language specification does not say “how it is done” for built-in types and built-in operators. The language simply says that the result of binary
+for built-in types is an rvalue – the sum of the operand values. That’s it. There’s no step-by-step description of what happens when a built-in operator is used (with some exceptions like&&,,etc.).The reason you can come up with a step-by-step description of how an overloaded operator works (which is what you have in your question) is because the process of evaluating overloaded operator comes through several sequence points. A sequence point in a C++ program implements the concept of discrete time: it is the only thing that separates something that happens before from things that happen after. Without a separating sequence point, there’s no “before” and no “after”.
In case of an overloaded operator, there quite a few sequence points involved in the process of its evaluation, which is why you can describe this process as a sequence of steps. The process of evaluation of built-in operator
+has no sequence points in it, so there absolutely no way to describe what happens there in step-by-step fashion. From the language point of view, the built-in+is evaluated through a blurry indivisible mix of unspecified actions that produce the correct result.It is done this way to give the compiler better optimization opportunities when evaluating built-in operators.