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 

swapping std::pairs?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Azumanga
Guest





PostPosted: Tue Sep 14, 2004 3:19 pm    Post subject: swapping std::pairs? Reply with quote



Why doesn't std::pair have an overload of swap, which swaps both of it's
elements? This would seem to be a natural thing to do, and allow for a
more efficent implementation of swap when the elements of the pair can
themselves be more efficently swapped.

In a similar note, the new "tuple" class in tr1 has a similar problem.

I can't see how this could lead to inefficency or a problem, but I
assume it must be possible?

Thank you,

Chris

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Daniel Krügler
Guest





PostPosted: Thu Sep 16, 2004 3:11 pm    Post subject: Re: swapping std::pairs? Reply with quote



Hello Azumanga,

Azumanga schrieb:

Quote:
Why doesn't std::pair have an overload of swap, which swaps both of
it's elements? This would seem to be a natural thing to do, and allow
for a more efficent implementation of swap when the elements of the
pair can themselves be more efficently swapped.

In a similar note, the new "tuple" class in tr1 has a similar problem.

I can't see how this could lead to inefficency or a problem, but I
assume it must be possible?


Both std::pair and tr1::tuple are heterogenous containers which store
their elements
by value (and **not** via a handle/pointer). How do you propose swapping
two instances of
arbitrary types?

If you solve the problem to implement a generally valid heterogenous swap

template <typename T1, typename T2>
void swap(T1& a, T2& b);

you also have solved the missing swap problems of pair and tuple ;-)

Furtheron, lets assume, you provide such a solution, e.g. using
boost::lexical_cast or
similar stuff: What is the general advantage of such a function? To my
opinion we will
end with something like

template <typename Policy, typename T1, typename T2>
void swap(T1& a, T2& b);

of at least one policy to decide, in which way the transaction shall
take place (what is the
intermediate storage?).

Now considering the fact that a class-defined swap member function is
usually of special
interest, if at least some of the class internals are of handle-like
nature, I don't see this
advantage in case of value-based heterogenous containers like pair and
tuple.

Greetings from Bremen,

Daniel Krügler


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Joe Gottman
Guest





PostPosted: Thu Sep 16, 2004 9:13 pm    Post subject: Re: swapping std::pairs? Reply with quote




"""Daniel Krügler (ne Spangenberg)""" <dsp (AT) bdal (DOT) de> wrote


Quote:
Both std::pair and tr1::tuple are heterogenous containers which store their
elements
by value (and **not** via a handle/pointer). How do you propose swapping
two instances of
arbitrary types?

Delegate to the types themselves.

template <class X, class Y>
void swap(std::pair<X,Y> &p1, std::pair<X, Y> &p2)
{
swap(p1.first, p2.first);
swap(p1.second, p2.second);
}

If either X or Y has a specialized swap() function defined, this should call
that function.


Joe Gottman


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
chris
Guest





PostPosted: Fri Sep 17, 2004 6:15 am    Post subject: Re: swapping std::pairs? Reply with quote

Daniel Krügler (ne Spangenberg) wrote:

Quote:
Hello Azumanga,

Azumanga schrieb:

Why doesn't std::pair have an overload of swap, which swaps both of
it's elements? This would seem to be a natural thing to do, and allow
for a more efficent implementation of swap when the elements of the
pair can themselves be more efficently swapped.

In a similar note, the new "tuple" class in tr1 has a similar problem.

I can't see how this could lead to inefficency or a problem, but I
assume it must be possible?



Both std::pair and tr1::tuple are heterogenous containers which store
their elements
by value (and **not** via a handle/pointer). How do you propose swapping
two instances of
arbitrary types?

If you solve the problem to implement a generally valid heterogenous swap

template <typename T1, typename T2
void swap(T1& a, T2& b);

you also have solved the missing swap problems of pair and tuple ;-)

Furtheron, lets assume, you provide such a solution, e.g. using
boost::lexical_cast or
similar stuff: What is the general advantage of such a function? To my
opinion we will
end with something like

template void swap(T1& a, T2& b);

of at least one policy to decide, in which way the transaction shall
take place (what is the
intermediate storage?).

Now considering the fact that a class-defined swap member function is
usually of special
interest, if at least some of the class internals are of handle-like
nature, I don't see this
advantage in case of value-based heterogenous containers like pair and
tuple.

I'm not sure if one of us is confused.. it might be me Smile


what I am suggesting is introducing into std something much like (sorry
if this isn't actually correct code, but I hope it's fairly close!)

template void swap(pair<T,U> &a,pair<T,U> &b) {
swap(a.first,b.first);
swap(a.second,b.second);
}

At the moment if I call swap on a pair, it simply falls through to the
"basic" swap, which does {pair<T,U> temp=a; a=b; b=temp;}. I can't see
how adding this specialised swap could ever hurt matters, and if one or
both of T or U has it's own specialised swap, then this would improve
performance...

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Alberto Barbati
Guest





PostPosted: Fri Sep 17, 2004 6:15 am    Post subject: Re: swapping std::pairs? Reply with quote

Daniel Krügler (ne Spangenberg) wrote:

Quote:
Hello Azumanga,

Azumanga schrieb:

Why doesn't std::pair have an overload of swap, which swaps both of
it's elements? This would seem to be a natural thing to do, and allow
for a more efficent implementation of swap when the elements of the
pair can themselves be more efficently swapped.

In a similar note, the new "tuple" class in tr1 has a similar problem.

I can't see how this could lead to inefficency or a problem, but I
assume it must be possible?


Both std::pair and tr1::tuple are heterogenous containers which store
their elements
by value (and **not** via a handle/pointer). How do you propose swapping
two instances of
arbitrary types?

If you solve the problem to implement a generally valid heterogenous swap

template <typename T1, typename T2
void swap(T1& a, T2& b);

you also have solved the missing swap problems of pair and tuple ;-)


IIUC, the OP did not intend to swap the two members of one pair, but to
swap two pairs (of the same type) memberwise. That is:

template void swap(pair<T1, T2>& p1, pair<T1, T2>& p2)
{
swap(p1.first, p2.first);
swap(p1.second, p2.second);
}

Notice that there are no swaps between heterogenous types.

The OP is surprised, as I am, that such an overload of std::swap is not
standardized. Without this overload swapping pairs falls back on the
generic std::swap that requires copies of both arguments even if one or
both have a more efficient way of swapping.

Alberto

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Daniel Krügler
Guest





PostPosted: Fri Sep 17, 2004 4:26 pm    Post subject: Re: swapping std::pairs? Reply with quote

Hello Joe Gottman,

Joe Gottman schrieb:

Quote:
"""Daniel Krügler (ne Spangenberg)""" <dsp (AT) bdal (DOT) de> wrote in message
news:4147EBC2.5000208 (AT) bdal (DOT) de...



Both std::pair and tr1::tuple are heterogenous containers which store their
elements
by value (and **not** via a handle/pointer). How do you propose swapping
two instances of
arbitrary types?



Delegate to the types themselves.

template <class X, class Y
void swap(std::pair {
swap(p1.first, p2.first);
swap(p1.second, p2.second);
}

If either X or Y has a specialized swap() function defined, this should call
that function.



OK, I obviously misunderstood the OP's intend, because I interpreted him
as wishing a heterogenous
swap between first and second.

Thanks for clarification,

Daniel

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Jonathan Biggar
Guest





PostPosted: Fri Sep 17, 2004 6:13 pm    Post subject: Re: swapping std::pairs? Reply with quote

Alberto Barbati wrote:
Quote:
IIUC, the OP did not intend to swap the two members of one pair, but to
swap two pairs (of the same type) memberwise. That is:

template <class T1, class T2
void swap(pair {
swap(p1.first, p2.first);
swap(p1.second, p2.second);
}

Notice that there are no swaps between heterogenous types.

The OP is surprised, as I am, that such an overload of std::swap is not
standardized. Without this overload swapping pairs falls back on the
generic std::swap that requires copies of both arguments even if one or
both have a more efficient way of swapping.

One thing that complicates the issue is exception safety. If the second
swap() operation throws in your template function, it leaves things in
an inconsistent state, with neither p1 nor p2 containing a consistent pair.

The generic std::swap does not have this problem.

--
Jon Biggar
Floorboard Software
[email]jon (AT) floorboard (DOT) com[/email]
[email]jon (AT) biggar (DOT) org[/email]

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
David B. Held
Guest





PostPosted: Sat Sep 18, 2004 12:03 am    Post subject: Re: swapping std::pairs? Reply with quote

Jonathan Biggar wrote:
Quote:
[...]
One thing that complicates the issue is exception safety. If the second
swap() operation throws in your template function, it leaves things in
an inconsistent state, with neither p1 nor p2 containing a consistent pair.

The generic std::swap does not have this problem.

Swap should probably not throw in the first place, and I can't think
of a sane implementation that does.

Dave

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
chris
Guest





PostPosted: Sat Sep 18, 2004 4:56 am    Post subject: Re: swapping std::pairs? Reply with quote

Jonathan Biggar wrote:
Quote:
Alberto Barbati wrote:

IIUC, the OP did not intend to swap the two members of one pair, but
to swap two pairs (of the same type) memberwise. That is:

template <class T1, class T2
void swap(pair {
swap(p1.first, p2.first);
swap(p1.second, p2.second);
}

Notice that there are no swaps between heterogenous types.

The OP is surprised, as I am, that such an overload of std::swap is
not standardized. Without this overload swapping pairs falls back on
the generic std::swap that requires copies of both arguments even if
one or both have a more efficient way of swapping.


One thing that complicates the issue is exception safety. If the second
swap() operation throws in your template function, it leaves things in
an inconsistent state, with neither p1 nor p2 containing a consistent pair.

The generic std::swap does not have this problem.


While this swap would have this problem, normal swap can still have the
problem that when you try to swap a and b you end up with both being
assigned the value of b, which is surely no better?

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Alberto Barbati
Guest





PostPosted: Sat Sep 18, 2004 4:56 am    Post subject: Re: swapping std::pairs? Reply with quote

Jonathan Biggar wrote:
Quote:
Alberto Barbati wrote:

One thing that complicates the issue is exception safety. If the second
swap() operation throws in your template function, it leaves things in
an inconsistent state, with neither p1 nor p2 containing a consistent pair.

Correct.


Quote:
The generic std::swap does not have this problem.

Incorrect. The generic std::swap still suffer this problem. The generic

swap looks like this:

template <class T>
void swap(T& a, T& b)
{
T t = a;
a = b;
b = t;
}

and pairs are copied memberwise. So swapping two pairs is performed like
this:

1) t.first = a.first
2) t.second = a.second
3) a.first = b.first
4) a.second = b.second
5) b.first = t.first
6) b.second = t.second

If one of the assignments in steps 4 or 6 throws an exception, you are
left with one of the two pairs in an unconsistent state. Yes, one broken
pair is better than two broken pairs, but it's still no consolation.

A memberwise swap is not less secure than the generic one. In fact, I
expect it to be more secure in the general case, because it is usually
easier to write a non-throwing swap than it is to write a non-throwing
assignment operator.

Alberto

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Jon Biggar
Guest





PostPosted: Mon Sep 20, 2004 5:22 am    Post subject: Re: swapping std::pairs? Reply with quote

chris wrote:
Quote:
One thing that complicates the issue is exception safety. If the
second swap() operation throws in your template function, it leaves
things in an inconsistent state, with neither p1 nor p2 containing a
consistent pair.

The generic std::swap does not have this problem.


While this swap would have this problem, normal swap can still have the
problem that when you try to swap a and b you end up with both being
assigned the value of b, which is surely no better?

Sure it's better. At least the first and second in both copies of b are
consistent with each other. With the proposed template specialized
version, you get a's first with b's second and vice versa.

--
Jon Biggar
Floorboard Software
[email]jon (AT) floorboard (DOT) com[/email]
[email]jon (AT) biggar (DOT) org[/email]

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Daniel Krügler
Guest





PostPosted: Mon Sep 20, 2004 10:44 am    Post subject: Re: swapping std::pairs? Reply with quote

Hello David Held,

David B. Held schrieb:

Quote:
Jonathan Biggar wrote:

[...]
One thing that complicates the issue is exception safety. If the
second swap() operation throws in your template function, it leaves
things in an inconsistent state, with neither p1 nor p2 containing a
consistent pair.

The generic std::swap does not have this problem.


Swap should probably not throw in the first place, and I can't think
of a sane implementation that does.


That is not true. It is not so unusual, that a copy constructor or copy
assignment
operator might throw under special circumstances. This usually happens,
if the
corresponding class uses some (limited) resources, and where the
"allocation"
for such a resource fails. Since the generic swap is implemented in
terms of these
members, it might throw. In most cases such a throwing swap can be prevented
by providing a user-defined spezialized swap for the given class, but I
don't think
that we can enforce that requirement for any user-defined class ("If you
don't provide
such a non-throwing swap for your class, you will have undefined
behaviour").

Greetings from Bremen,

Daniel

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Peter Dimov
Guest





PostPosted: Mon Sep 20, 2004 1:07 pm    Post subject: Re: swapping std::pairs? Reply with quote

[email]jon (AT) floorboard (DOT) com[/email] (Jon Biggar) wrote in message news:<BR%2d.21110$By7.18254 (AT) newssvr27 (DOT) news.prodigy.com>...
Quote:
chris wrote:
One thing that complicates the issue is exception safety. If the
second swap() operation throws in your template function, it leaves
things in an inconsistent state, with neither p1 nor p2 containing a
consistent pair.

The generic std::swap does not have this problem.


While this swap would have this problem, normal swap can still have the
problem that when you try to swap a and b you end up with both being
assigned the value of b, which is surely no better?

Sure it's better. At least the first and second in both copies of b are
consistent with each other.

void swap( pair<T1, T2> & p1, pair<T1, T2> & p2 )
{
pair<T1, T2> tmp( p1 ); // OK
p1 = p2; // p1.second = p2.second throws
p2 = tmp;
}

Now you have p1.first == p2.first, and the state of p1.second is
unspecified (basic) or unchanged (strong). No?

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Alberto Barbati
Guest





PostPosted: Tue Sep 21, 2004 5:22 am    Post subject: Re: swapping std::pairs? Reply with quote

Jon Biggar wrote:
Quote:
chris wrote:

One thing that complicates the issue is exception safety. If the
second swap() operation throws in your template function, it leaves
things in an inconsistent state, with neither p1 nor p2 containing a
consistent pair.

The generic std::swap does not have this problem.


While this swap would have this problem, normal swap can still have
the problem that when you try to swap a and b you end up with both
being assigned the value of b, which is surely no better?


Sure it's better. At least the first and second in both copies of b are
consistent with each other.

That's not entirely correct. "Normal" swap can indeed break one of two
copies (but not both). See my other post for details.

Alberto

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards 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.