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,973 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to Gene Bushuyev   
   Re: Where are iterators for rvalue conta   
   02 Mar 12 12:42:49   
   
   178b9c1e   
   From: daniel.kruegler@googlemail.com   
      
   On 2012-03-02 11:41, Gene Bushuyev wrote:   
   > I was looking at the C++11 standard docs and didn't see iterators for   
   > rvalue containers.   
      
   There exist a general adapter for any forward iterator to a "moving   
   iterator", namely std::move_iterator.   
      
   > For example, std::vector has only these begin()   
   > member functions:   
   >   
   > iterator begin();   
   > const_iterator begin() const;   
   >   
   > Where is "iterator begin()&&;" ?   
      
   It does not exist, for good reasons:   
      
   1) You cannot overload ref-qualified and non-ref-qualified member   
   functions, so simply adding begin() &&; would make instantiating vector   
   for these functions ill-formed.   
      
   2) They are not needed, because you can call a non-ref-qualified from an   
   rvalue.   
      
   3) It would not help solving your problem to provide begin()&& returning   
   an iterator, because the root of your problem is that the iterator's   
   indirection operator returns an lvalue, but you need one with an rvalue.   
      
   > Say, I want to construct my container by moving from a vector,   
   >   
   > MyContainer::MyContainer(vector&&  v)   
   > {   
   >     reserve(v.size());   
   >     uninitialized_copy(move(v).begin(), move(v).end(), data);   
   >     size = v.size();   
   > }   
   >   
   > But that would copy instead of moving, because containers have no   
   > rvalue iterators. Am I missing something or the standard missed them?   
      
   The standard did not miss to add them. It would be possible to follow   
   your suggestion with the cost of   
      
   a) replacing all container begin/end pairs by a double-pair each one   
   &-ref-qualified and &&-ref-qualified   
      
   b) doubling the number of iterators with in each container:   
   &-ref-qualified begin/end functions return the normal one,   
   &&-ref-qualified returning a moving iterator instead.   
      
   Instead the standard chose a more economic and useful feature:   
   std::move_iterator. Just replace your code above by   
      
   MyContainer::MyContainer(vector&& v)   
   {   
      reserve(v.size());   
      uninitialized_copy(std::make_move_iterator(v.begin()),   
   std::make_move_iterator(v.end()), data);   
      size = v.size();   
   }   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
   --   
         [ 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