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::re size being as follows:
template<class _Ty, class _Ax = allocator<_Ty> >
class vector : public _Vector_val<_Ty , _Ax>
{
public:
....
void resize(size_typ e _Newsize)
{
// determine new length, padding with _Ty() elements as needed
resize(_Newsize , _Ty());
}
void resize(size_typ e _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_typ e _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 0 1301 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 rgbReserved;
|
by: Alex Vinokur |
last post by:
---------
#include <vector>
using namespace std;
struct Foo
{
Foo (int) {}
};
int main ()
{
vector<Foo> v1;
|
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 std::vector::erase, it calls T::operator= only three times no matter
|
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 variable "tri" is an
input arg to my function using the syntax:
function(vector<vector<int> >& tri)
The problem occurs when the tri 'matrix' is resized to triple or
|
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 times!
The (relevant) snippets of code are below : since it's relevant though
L=28,T=96 (so TasteProps weighs in at a hefty
8*96*28*28*28*96=1,618,477,056 doubles )
| |
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 stated that everyone else was taking 2 or 3 seconds.
Then he found one other person taking >30 seconds, and it turns out the
common denominator was both of us have Intel chips (Celeron) where the other
people have AMD.
I had him send me his code...
|
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 advantage/disadvantage of using one over the other?
I checked the VS7.1 implementation, in which I couldn't find any
difference. Both of them are effectively calling erase(begin(), end()).
|
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 instance
for the copy constructor).
For example:
C++
vector<classvClass;
|
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 assignment is done of the
referenced OtherObj object (according to the compiler messages). This is
strange, since the referenced OtherObj is initialized using member
initialization lists. Anyone a clue?
thanks,
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |