Here is the code from the book
LargeType randomItem1( const vector<LargeType> & arr )
{
return arr[ randomInt( 0, arr.size( )-1)];
}
const LargeType & randomItem2( const vector<LargeType> & arr )
{
return arr[ randomInt( 0, arr.size( )-1)];
}
vector<LargeType> vec;
...
LargeType item1 = randomItem1( vec );// copy
LargeType item2 = randomItem2( vec );// copy
const LargeType & item3 = randomItem2( vec ); // no copy
vector<int> partialSum( const vector<int> & arr )
{
vector<int> result( arr.size( ) );
result[ 0 ] = arr[ 0 ];
for( int i = 1; i < arr.size( ); ++i )
{
result[ i ] = result[ i-1]+ arr[ i ];
}
return result;
}
vector<int> vec;
...
vector<int> sums = partialSum( vec ); // Copy in old C++; move in C++11
The book says LargeType randomItem1( const vector<LargeType> & arr )
does not call move semantics while vector<int> partialSum( const vector<int> & arr )
does. Why is this happening?
I understand that return arr[ randomInt( 0, arr.size( )-1)];
is an lvalue since arr
itself is reference of object but isn't result
an object too? the book says return result
is temporary, however it is declared at line 3 at second code box.
want to know why return result is temporary even if it is declared and have name.