473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

vector::resize parameters

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


Jul 22 '05 #1
0 1282

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Peter Mrosek | last post by:
Hello, I have the following declaration in an header file (abbreviated version): typedef struct { unsigned char rgbBlue; unsigned char rgbGreen; unsigned char rgbRed; unsigned char...
9
by: Alex Vinokur | last post by:
--------- #include <vector> using namespace std; struct Foo { Foo (int) {} }; int main () { vector<Foo> v1;
8
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike...
2
by: mj | last post by:
Hi, I recently have found it necessary to move from fortran to c++ for scientific programming... I'm working on a program that needs to resize a 2d vector of vectors within a function... This...
2
by: mrbrightsidestolemymoney | last post by:
Hi, I'm having a problem resizing a (very big) nested vector. It's not the most streamlined piece of code ever but I need this array to avoid having to recalculate the same quantity millions of...
3
by: Jim Langston | last post by:
I really am not sure if this question belongs in this newsgroup, but not sure where else to ask it. There is someone working on a game that I tested, and it was taking >30 seconds to load. He...
5
by: atreya | last post by:
Hi, I'm trying to figure out if there are any differences between the implementation of resize(0) and clear() that a programmer using std::vector should be aware of! Could there be any...
6
by: Jeff.Boeker | last post by:
I'm learning a lesson in how I need to be more specific :) In C++ I can resize a vector and it will allocate memory and it will call the default constructor if necessary (or I can supply an...
3
by: Bram Kuijper | last post by:
Hi all, I am trying to resize a vector of objects (MyObj below), which contain references to other objects (OtherObj, see below). However, apparently somewhere in the resize operation an...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.