vector::resize parameters

S

solosnake

Hello, and merry Christmas!

When trying to compile some code that had specific alignment requirements, I
found that the Visual Studio .NET compiler gave me compile time errors. It
forbids alignment specification of stack variables.

Upon examing the source of the error, it was due to the signatures of the
std::vector::resize being as follows:

template<class _Ty, class _Ax = allocator<_Ty> >
class vector : public _Vector_val<_Ty, _Ax>
{
public:

....

void resize(size_type _Newsize)
{
// determine new length, padding with _Ty() elements as needed
resize(_Newsize, _Ty());
}

void resize(size_type _Newsize, _Ty _Val)
{
// determine new length, padding with _Val elements as needed
if (size() < _Newsize)
_Insert_n(end(), _Newsize - size(), _Val);
else if (_Newsize < size())
erase(begin() + _Newsize, end());
}

The second version is passing the "_Val" object by copy, instead of what I
would have assumed to be a more conventional method of by const referance.
This to me appears unnecessary, as the recipient of the "_Val" object in the
call, the "_Insert_n" method, actually takes as a parameter a const
reference.

I have changed the signature of "resize" to as follows:

void resize(size_type _Newsize, const _Ty& _Val)


....and everything works as I hoped and expected. The compilers alignment
requirements are not breached, and I have also saved a potentially expensive
and unnecessary copy.

Can anyone tell me if there was a good reason why the original version
passed by copy instead of by const reference?
Have I broken anything serious by doing so (other then possible portability
issues)?
Does the standard specify passing by copy, and if so why?

Thanks in advance for all replies!

- solosnake
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top