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,169 of 33,346   
   Dave Abrahams to All   
   Re: specializing swap for nested iterato   
   21 Apr 12 13:29:37   
   
   From: dave@boostpro.com   
      
   on Sat Apr 21 2012,  wrote:   
      
   > { 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 Allocator>::iterator& x,   
   >                        typename myContainer Allocator>::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.   
      
   Seems not...   
      
   On the other hand, it's a very bad idea to write a user-defined swap   
   with different semantics from std::swap.  If the *output* of that   
   program can distinguish which swap is being used, you have bigger   
   problems.   
      
   How /are/ you determining which swap is being used?   
      
   > 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?   
      
   Seems like a bug.  What's your compiler?   
      
   > 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?   
      
   You might try making it a nested friend:   
      
        namespace ns {   
          template<   
            class Key,   
            class Compare = std::less,   
            class Allocator = std::allocator   
          >   
          class myContainer {   
            public:   
              class iterator_implementation {   
                friend void swap(   
                  iterator_implementation& a, iterator_implementation& b)   
                {   
                    // do your business here...   
                }   
                public:   
                  inline iterator_implementation() {   
                  }   
              };   
            public:   
              typedef iterator_implementation iterator;   
          };   
      
   --   
   Dave Abrahams   
   BoostPro Computing   
   http://www.boostpro.com   
      
      
         [ 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