home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,849 of 33,346   
   Ulrich Eckhardt to All   
   Re: Add an explicit specialization of st   
   25 Jan 12 11:26:00   
   
   f4e6ea45   
   From: ulrich.eckhardt@dominolaser.com   
      
   Am 24.01.2012 23:04, schrieb vlad.moscow@mail.ru:   
   > For example if someone wants to add to std namespace a specialization   
   > of std::swap for a user type.   
   >   
   > So 1) Does the Standard allow to do so? 2) If evven it allows to do so   
   > it is a good practise or it shall be escaped?   
      
   Concerning swap, which I consider an important complementary to ctor,   
   cctor, dtor and operator=, I usually take this approach:   
      
   namespace N {   
      class X {};   
      void swap(X&, X&);   
   }   
      
   When using it, I typically take this approach:   
      
      void foo() {   
        N::X x1, x2;   
        swap(x1, x2);   
      }   
      
   In generic code, I rather do this:   
      
      template   
      void foo() {   
        using std::swap;   
        T t1, t2;   
        swap(t1, t2);   
      }   
      
   In both cases, I'm relying on ADL to find the right swap() overload. In   
   the second case, I'm importing std::swap as fallback for the case that   
   ADL doesn't turn up anything tailored to the actual type.   
      
      
   Just wondering, why isn't there an FAQ entry? As already mentioned   
   above, swap() is an important function like ctor and dtor, and getting   
   it right can be tricky. Especially the OP's case concerning where to put   
   the overload or the second case above where blindly calling std::swap()   
   would have been the wrong thing. What else is there to pay attention to?   
      
      
   Uli   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca