programminga general overview vector veclargetype item randomitem vec )largetype item randomitem vec )const largetype item randomitem vec )largetype randomitem const vector arr return arrrandomint arr size ]const largetype randomitem const vector arr return arrrandomint arr size ]/copy /copy /no copy figure two versions to obtain random item in an arraysecond version avoids creation of temporary largetype objectbut only if caller accesses it with constant reference firstconsider two implementations of randomitem the first implementationshown in lines - of figure uses return-by-value as resultthe largetype at the random array index will be copied as part of the return sequence this copy is done becausein generalreturn expressions could be rvalues ( return + and hence will not logically exist by the time the function call returns at line but in this casethe return type is an lvalue that will exist long after the function call returnssince arr is the same as vec the second implementation shown at lines - takes advantage of this and uses returnby-constant-reference to avoid an immediate copy howeverthe caller must also use constant reference to access the return valueas shown at line otherwisethere will still be copy the constant reference signifies that we do not want to allow changes to be made by the caller by using the return valuein this case it is needed since arr itself is non-modifiable vector an alternative is to use auto at line to declare item figure illustrates similar situation in which call-by-value was inefficient in classic +due to the creation and eventual cleanup of copy historicallyc+programmers have gone to great extent to rewrite their code in an unnatural wayusing techniques involving pointers or additional parameters that decrease readability and maintainabilityeventually leading to programming errors in ++ objects can define move semantics that can be employed when return-by-value is seenin effectthe result vector will be moved to sumsand the vector implementation is optimized to allow this to be done with little more than pointer change this means that partialsum as shown in figure can be expected to avoid unnecessary copying and not need any changes the details on how move semantics are implemented are discussed in section vector implementation is discussed in section notice that the move semantics can be called on result at line in figure but not on the returned expression at line in figure this is consequence of the distinction between temporary and non-temporaryand the distinction between an lvalue reference and an rvalue reference