c++ - Using local variable assigned to return value of a function or using function directly -
is there difference between this:
myclass c = getmyclass(); calculate(c.value);
and this:
calculate(getmyclass().value);
in scope of performance , memory allocation?
yes, there serious difference. in first case, c
lvalue, , not destroyed until end of scope. can delay useful things reosurce cleanup. more problematically, beingness lvalue means cannot moved, must copied, inefficient many classes downright illegal of import classes, unique_ptr
.
in sec case, compiler cleans temporary right away, resources released promptly, , beingness rvalue gives compiler more freedom optimize , permits move semantics.
this still true when value
fellow member or fellow member function, result of both can inherit value category parent object.
you should scope objects minimum scope required, , if don't need access c
later, should utilize temporary.
c++ performance compiler-construction
No comments:
Post a Comment