Why does the internal buffer of tmp have the same address of the returned element?
I assume that by returned "element", you mean the internal buffer of the returned string object.
It is unclear whether you refer to the internal address of tmp
before the assignment or after the assignment. In practice, the address typically remains the same in this case, and thus the address would not be the same as the address of the returned string's buffer.
The move assignment could change tmp
to have the internal address of the returned string. In practice, this won't happen unless the string is long enough to exceed the short string optimisation.
As for why: Because that's how the move assignment of std::string
was implemented by your standard library.
just simply assigned to the pointer of the returned value due to RVO?
There may indeed be RVO on the line return std::string();
. RVO has no effect on the internal address of tmp
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…