Any good way to extend std::swap?

Discussion in 'C++' started by Sung Jin Hwang, Jan 20, 2006.

  1. 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.
     
    Sung Jin Hwang, Jan 20, 2006
    #1
    1. Advertisements

  2. Sung Jin Hwang

    red floyd Guest

    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".
     
    red floyd, Jan 20, 2006
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    7,002
    Ivan Vecerina
    Mar 17, 2005
  2. Vinu
    Replies:
    4
    Views:
    555
    Jim Langston
    Jul 7, 2005
  3. Vinu
    Replies:
    0
    Views:
    499
  4. Jason Heyes
    Replies:
    8
    Views:
    1,023
    Andrew Koenig
    Jan 15, 2006
  5. Geoffrey S. Knauth
    Replies:
    6
    Views:
    1,308
    Earl Purple
    Jan 18, 2006
  6. puzzlecracker
    Replies:
    3
    Views:
    2,307
    Mike Wahler
    May 8, 2006
  7. Niels Dekker (no reply address)

    What swap is called when using std::swap?

    Niels Dekker (no reply address), Jul 19, 2006, in forum: C++
    Replies:
    4
    Views:
    1,258
    Niels Dekker (no reply address)
    Jul 20, 2006
  8. David T
    Replies:
    3
    Views:
    628
    Alf P. Steinbach
    Mar 8, 2009
Loading...