I think this is shabby re-allocation on the MS compiler (it is MS we're
talking about here right?)
if you provide a default constructor for X, and replace your main with:
int main(int argc, char** argv)
{
const X & x1(100);
vector<X> v;
v.reserve(1);
std::cout << "\n\n" << std::endl;
v.push_back(x1);
return 0;
}
You can get a clearer idea of where the copies are taking place - explicitly
instantiating as a reference removes any copy it may have caused when
created via the push_back parameter - and yet if you remove the reserve(1)
the same behaviour you are seeing repeats - which means in my book it's just
bad re-allocation when inserting in the vector.
regards,
Aiden.
<ri***********@yahoo.co.uk> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
The output of this:
#include <iostream>
#include <vector>
using namespace std;
struct X {
int i;
X(const X& x) : i(x.i) {
cout << "ctor copy: " << i << endl;
}
X(int ii) : i(ii) {
cout << "ctor by int: " << i << endl;
}
~X() {
cout << "dtor: " << i << endl;
}
};
int main(int argc, char **argv) {
vector<X> v;
v.push_back(X(100));
return 0;
}
Is this:
ctor by int: 100
ctor copy: 100
ctor copy: 100
dtor: 100
dtor: 100
dtor: 100
When you do the push_back, I can understand the vector wanting to make
its own copy, but why is this apparently done twice?
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com