 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Lasse Skyum Guest
|
Posted: Thu Oct 30, 2003 3:53 pm Post subject: clearing capacity af a vector |
|
|
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
--
Lasse
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Oct 30, 2003 4:01 pm Post subject: Re: clearing capacity af a vector |
|
|
"Lasse Skyum" <no spam> wrote...
| Quote: | 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
|
|
| Back to top |
|
 |
Chris Theis Guest
|
Posted: Thu Oct 30, 2003 4:03 pm Post subject: Re: clearing capacity af a vector |
|
|
"Lasse Skyum" <no spam> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
Andrew Koenig Guest
|
Posted: Thu Oct 30, 2003 4:13 pm Post subject: Re: clearing capacity af a vector |
|
|
| Quote: | 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.
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Oct 30, 2003 5:11 pm Post subject: Re: clearing capacity af a vector |
|
|
"Andrew Koenig" <ark (AT) acm (DOT) org> wrote...
| Quote: | 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.
|
Thank you for the correction. I ought to check before posting.
Victor
|
|
| Back to top |
|
 |
Lasse Skyum Guest
|
Posted: Thu Oct 30, 2003 6:01 pm Post subject: Re: clearing capacity af a vector |
|
|
| Quote: | 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)
--
Lasse
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Oct 30, 2003 6:33 pm Post subject: Re: clearing capacity af a vector |
|
|
"Lasse Skyum" <no spam> wrote...
| Quote: |
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)
|
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
|
|
| Back to top |
|
 |
Lasse Skyum Guest
|
Posted: Thu Oct 30, 2003 6:47 pm Post subject: Re: clearing capacity af a vector |
|
|
| Quote: | 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?
--
Lasse
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Oct 30, 2003 7:25 pm Post subject: Re: clearing capacity af a vector |
|
|
"Lasse Skyum" <no spam> wrote...
| Quote: |
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?
|
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
|
|
| Back to top |
|
 |
Lasse Skyum Guest
|
Posted: Thu Oct 30, 2003 7:50 pm Post subject: Re: clearing capacity af a vector |
|
|
| Quote: | 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 :-)
--
Lasse
|
|
| Back to top |
|
 |
Howard Hinnant Guest
|
Posted: Mon Nov 03, 2003 9:02 pm Post subject: Re: clearing capacity af a vector |
|
|
In article <jDbob.47350$275.116409@attbi_s53>,
"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote:
| Quote: | "Andrew Koenig" <ark (AT) acm (DOT) org> wrote...
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.
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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|