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 32,164 of 33,346   
   galexander2@nc.rr.com to All   
   specializing swap for nested iterator cl   
   21 Apr 12 00:51:07   
   
   { Please limit your lines to 70 characters -mod }   
      
   I'm developing my own container class and have implemented the   
   container's iterator as a nested class.  I want to include a   
   specialized version of swap for the iterator.  Here's a small example   
   of what I tried:   
      
       #include    
       #include    
       #include    
       #include    
       #include    
      
       namespace ns {   
         template, class   
   Allocator = std::allocator >   
         class myContainer {   
           public:   
             class iterator_implementation {   
               public:   
                 inline iterator_implementation() {   
                 }   
             };   
           public:   
             typedef iterator_implementation iterator;   
         };   
         template    
         inline void swap(typename myContainer::iterator& x,   
                          typename myContainer::iterator& y) {   
           std::cout << "**** specialized iterator swap ****" << std::endl;   
         }   
       }   
      
       int main(int argc, char* argv[]) {   
         ns::myContainer::iterator x = ns::myContainer::iterator();   
         ns::myContainer::iterator y = ns::myContainer::iterator();   
         using std::swap;   
         std::cout << "before swap(x, y)" << std::endl;   
         swap(x, y);   
         std::cout << "after swap(x, y)" << std::endl;   
         std::cout << std::endl;   
         std::cout << "before swap, std::allocator   
   >(x, y)" << std::endl;   
         swap, std::allocator >(x, y);   
         std::cout << "after swap, std::allocator   
   >(x, y)" << std::endl;   
         exit(0);   
       }   
      
   The problem is that swap(x, y) calls std::swap rather than the   
   specialized ns:swap.  It seems that ADL isn't finding ns:swap. To call   
   ns:swap, one has to specify template arguments as in for example   
   swap, std::allocator >(x, y), which isn't   
   what I want.  Is this correct behavior according to the C++ standard?   
   Or is it a compiler bug? If this is correct standard behavior, can   
   someone point to where this behavior is described?  In this case is   
   there a standard way to define a specialize swap for iterator short of   
   making the iterator_implementation class its own stand-alone class   
   rather than a nested class?   
      
   Thanks,   
   Geoff Alexander   
      
      
   --   
         [ 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