C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

clearing capacity af a vector

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Lasse Skyum
Guest





PostPosted: Thu Oct 30, 2003 3:53 pm    Post subject: clearing capacity af a vector Reply with 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

--
Lasse


Back to top
Victor Bazarov
Guest





PostPosted: Thu Oct 30, 2003 4:01 pm    Post subject: Re: clearing capacity af a vector Reply with quote



"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





PostPosted: Thu Oct 30, 2003 4:03 pm    Post subject: Re: clearing capacity af a vector Reply with quote




"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





PostPosted: Thu Oct 30, 2003 4:13 pm    Post subject: Re: clearing capacity af a vector Reply with quote

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





PostPosted: Thu Oct 30, 2003 5:11 pm    Post subject: Re: clearing capacity af a vector Reply with quote

"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





PostPosted: Thu Oct 30, 2003 6:01 pm    Post subject: Re: clearing capacity af a vector Reply with quote



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





PostPosted: Thu Oct 30, 2003 6:33 pm    Post subject: Re: clearing capacity af a vector Reply with quote

"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





PostPosted: Thu Oct 30, 2003 6:47 pm    Post subject: Re: clearing capacity af a vector Reply with quote


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





PostPosted: Thu Oct 30, 2003 7:25 pm    Post subject: Re: clearing capacity af a vector Reply with quote

"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





PostPosted: Thu Oct 30, 2003 7:50 pm    Post subject: Re: clearing capacity af a vector Reply with quote



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





PostPosted: Mon Nov 03, 2003 9:02 pm    Post subject: Re: clearing capacity af a vector Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.