468,734 Members | 2,184 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,734 developers. It's quick & easy.

Any good way to extend std::swap?


Hi.

Is there a good way to extend std::swap to user type classes?
Overloading can be a solution but when user type is not a
class but class template, we need partial specialization and
function partial specialization is not allowed.

What I can easily think about is to define a function in the
namespace which contains the user type but this forces to
use a namespace (well... though it is better to use namespaces)
and C++ symbol lookup rules becomes complex for some
cases and it is not fool-proof. Furthermore, it forces me to
use such redundancies when I wish to use std::swap.

{
using std::swap;
swap(A, B);
}

Is there any better suggestions for this?
By the way, is there any attemp to add an operator or
re-use an operator in C++ so that swap becomes a built-in
operator operation?

Swap or exchange is quite essential concept in computer
science and it is quite frustrating that using and implementing
swap operations is that tricky. In addition, touching and
modifying something that is in std namespace always makes
me frown and feel guilty.

Why don't we allocate an operator for swap operation
and let the compiler generate default code like assignment
operator? I sometimes think that it would be nice if colon
operator does this thing, though this will introduce ambiguity
in parsing when used with label feature.
Jan 20 '06 #1
1 2043
Sung Jin Hwang wrote:
Hi.

Is there a good way to extend std::swap to user type classes?
Overloading can be a solution but when user type is not a
class but class template, we need partial specialization and
function partial specialization is not allowed.


There's a description in full gory detail in "C++ Template Metaprogramming".

Jan 20 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Davis King | last post: by
5 posts views Thread by Steve Hill | last post: by
17 posts views Thread by beliavsky | last post: by
4 posts views Thread by Niels Dekker (no reply address) | last post: by
9 posts views Thread by ma740988 | last post: by
1 post views Thread by ma740988 | last post: by
22 posts views Thread by Pramod | last post: by
11 posts views Thread by Dennis Jones | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.