clearing capacity af a vector

L

Lasse Skyum

We had a thread earlies about how to clear the capacity af a std::vector...
how about this:

std::vector<int> v;

v.~vector<int>(); // destruct it (don't know if I need
the <int> here?)
new (&v)vector<int>(); // construct it again
 
V

Victor Bazarov

Lasse Skyum said:
We had a thread earlies about how to clear the capacity af a std::vector...
how about this:

std::vector<int> v;

v.~vector<int>(); // destruct it (don't know if I need
the <int> here?)
new (&v)vector<int>(); // construct it again

While it _might_ work (and even according some twisted reading
of the Standard, it is allowed to work), I'd suggest

v.swap(std::vector<int>());

Victor
 
C

Chris Theis

Lasse Skyum said:
We had a thread earlies about how to clear the capacity af a std::vector...
how about this:

std::vector<int> v;

v.~vector<int>(); // destruct it (don't know if I need
the <int> here?)
new (&v)vector<int>(); // construct it again

--

As Victor already said - this might work. But IMHO using placement new with
any real need to is not such a hot idea. BTW what's the problem with using
the "normal" solution with the swap function?

Chris
 
A

Andrew Koenig

While it _might_ work (and even according some twisted reading
of the Standard, it is allowed to work), I'd suggest
v.swap(std::vector<int>());

This technique almost works, but not quite. The trouble is that
std::vector<int>() is an rvalue, which means that it can't be used as the
argument to swap, which requires an lvalue.

You can make the technique work with a slightly sneaky trick:

std::vector<int>().swap(v);

This code relies on the fact that it is permissible to call a member
function that changes the contents of an rvalue.
 
V

Victor Bazarov

Andrew Koenig said:
This technique almost works, but not quite. The trouble is that
std::vector<int>() is an rvalue, which means that it can't be used as the
argument to swap, which requires an lvalue.

You can make the technique work with a slightly sneaky trick:

std::vector<int>().swap(v);

This code relies on the fact that it is permissible to call a member
function that changes the contents of an rvalue.

Thank you for the correction. I ought to check before posting.

Victor
 
L

Lasse Skyum

As Victor already said - this might work. But IMHO using placement new with
any real need to is not such a hot idea. BTW what's the problem with using
the "normal" solution with the swap function?

The only reason was, that I was told that the swap method was not guarenteed
to work by the standard. I thought this method would...

I don't really understand why it wouldn't work... the resulting object
should be a newly initialized vector. Just because it uses the same piece of
memory that once was used for another vector should not affect that...or?

Maybe it would be inefficient compared to the swap method? (Suppose it was
something that happened verry often)
 
V

Victor Bazarov

Lasse Skyum said:
The only reason was, that I was told that the swap method was not guarenteed
to work by the standard. I thought this method would...

I don't really understand why it wouldn't work... the resulting object
should be a newly initialized vector. Just because it uses the same piece of
memory that once was used for another vector should not affect that...or?

Maybe it would be inefficient compared to the swap method? (Suppose it was
something that happened verry often)

No, the efficiency of those two methods is very close. Swapping
variation is basically the same as constructing an empty one and
destroying the original one, with the addition of swapping some
data members, so swapping may actually be just a tad slower.

Victor
 
L

Lasse Skyum

No, the efficiency of those two methods is very close. Swapping
variation is basically the same as constructing an empty one and
destroying the original one, with the addition of swapping some
data members, so swapping may actually be just a tad slower.

Victor

Okay, seems strange that people don't do it that way then... I still don't
see why it wouldn't work?
 
V

Victor Bazarov

Lasse Skyum said:
Okay, seems strange that people don't do it that way then... I still don't
see why it wouldn't work?

Well, I don't want this to turn into another discussion why using
a destructor and then placement new isn't such a good idea, you
can find it on Google Groups, if you want to see what people think.
In short: if you have to call a destructor directly on something
you didn't create using placement new, you might need to rethink
your design or implementation. IMHO.

Victor
 
L

Lasse Skyum

In short: if you have to call a destructor directly on something
you didn't create using placement new, you might need to rethink
your design or implementation. IMHO.

Well that kind of makes sence... a little anyway :)
 
H

Howard Hinnant

Victor Bazarov said:
Thank you for the correction. I ought to check before posting.

That's a common mistake, even for experts such as yourself. For that
reason, I'd love to see:

template <class T>
void
swap(T&&, T&&);

(see move proposal:
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2002/n1377.htm
)

which would allow expressions such as:

swap(v, std::vector<int>());

or:

swap(std::vector<int>(), v);

which I've personally grown to like (ymmv).

-Howard
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top