I have a question, I've been going around for a few days, and I can not understand how it works or what I'm doing wrong.
Starting point:
Suppose we have the following function:
ArrayWrapper f(ArrayWrapper arr){
return arr
}
What my compiler does:
1- When passing through value, call the copy constructor, get arr , and return a temporary object arr .
2- We arrive at return
, and as arr
is temporary, and the function returns by value, because in this case it calls the constructor move and it returns my object.
Now my doubts come:
a) If it were my return
: return ArrayWrapper(arr);
-
Compiler behavior:
Call the copy constructor, get
arr
, and call the copy constructor again. (ArrayWrapper(arr)
, it's temporary or so I thought). -
What I expected to be
arr
temporary, inArrayWrapper(arr)
the constructor would be called move returning a temporary object, and then thereturn
when having to return a copy by value, call the move constructor again.
Why does the compiler call the constructor copy once it has already obtained arr
, and does not call constructor move twice, once to get the temporary object ArrayWrapper(arr)
and second to return it in return
?