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,008 of 33,346   
   Dave Abrahams to All   
   Re: Where are iterators for rvalue conta   
   15 Mar 12 00:07:00   
   
   27239687   
   From: dave@boostpro.com   
      
   on Wed Mar 14 2012, Gene Bushuyev  wrote:   
      
   > On Mar 2, 2:01 pm, Dave Abrahams  wrote:   
   >> on Fri Mar 02 2012, Gene Bushuyev  wrote:   
   >> > I was looking at the C++11 standard docs and didn't see iterators for   
   >> > rvalue containers. For example, std::vector has only these begin()   
   >> > member functions:   
   >>   
   >   
   >> > iterator begin();   
   >> > const_iterator begin() const;   
   >>   
   >> > Where is "iterator begin() &&;" ?   
   >>   
   >> > 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?   
   >>   
   >> I don't think you really want implicit move iterators for container   
   >> rvalues.  Move iterators only work for single-read traversals.  Replace   
   >> with some algorithm that makes multiple passes over the input data and   
   >> you could have a disaster on your hands.   
   >>   
   >   
   > Yes, I would like move iterators for rvalue containers,   
      
   Trust me, you really don't want that :-)   
      
   > because the perfect forwarding would work.   
      
   The problem is that too many other things would "work" (until runtime)   
   as well.   
      
   > And if algorithms makes multiple passes it shouldn't take container by   
   > r-value reference, or at least know when to move.   
      
   The standard library algorithms don't operate on containers; they   
   operate on iterators.  You'd have to, /at least/, make sure the "rvalue   
   iterators" were classified no more refined-ly than InputIterator to   
   avoid them being used in multiple-read situations.  And even that isn't   
   right; you can read an input iterator multiple times.  We really don't   
   have a proper concept for read-once, random-access iterators like   
   move_iterator, and until we do, you're better off being really explicit   
   about it when you want one.  move_iterator is best thought of as a   
   low-level convenience adaptor for implementing various moving algorithms   
   in terms of their non-moving cousins.   
      
   --   
   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