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. Advertising

  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. Advertising

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. Davis King

    std::swap and exceptions

    Davis King, Jul 20, 2003, in forum: C++
    Replies:
    2
    Views:
    379
    Davis King
    Jul 20, 2003
  2. Marcin Kalicinski

    Custom swap in std namespace

    Marcin Kalicinski, Dec 5, 2005, in forum: C++
    Replies:
    2
    Views:
    349
    red floyd
    Dec 5, 2005
  3. Jason Heyes
    Replies:
    8
    Views:
    769
    Andrew Koenig
    Jan 15, 2006
  4. 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,013
    Niels Dekker (no reply address)
    Jul 20, 2006
  5. David T
    Replies:
    3
    Views:
    368
    Alf P. Steinbach
    Mar 8, 2009
Loading...

Share This Page