From: dave@boostpro.com   
      
   on Mon Mar 26 2012, Marc wrote:   
      
   > Hello,   
   >   
   > in C++11, forward iterators may, when dereferenced, yield an rvalue   
   > reference. However, the standard describes them as suitable for   
   > multi-pass algorithms, and I am wondering how safe that is.   
      
   In general, that's not very safe, but it's really a problem with   
   concepts. The standard doesn't distinguish between multi-pass iterators   
   and multi-read iterators. That is, iterators that dereference as rvalue   
   references are fine for making multiple *passes* over the data as long   
   as you only *read* the data once.   
      
   > In 25.1p8 and p9, it says that (Binary)Predicate can't apply   
   > non-constant functions to a dereferenced iterator. So if I provide a   
   > function object that takes advantage of rvalue-references when it gets   
   > them, it is my fault that the algorithm will fail (can be surprising).   
   > However that wording doesn't seem to cover operator< and operator==   
   > themselves, only explicitly provided comparators.   
   >   
   > For some context, yes it can make sense to have an operator< that eats   
   > its rvalue arguments,   
      
   I guess I'll have to take your word for it.   
      
   > and for the last use of the elements in a container (say the last   
   > thing we care about is the index of some element), we'd like to pass a   
   > move_iterator to the algorithm, and let the algorithm decide by itself   
   > when it can take advantage of rvalue references (won't need that   
   > element again) and when it cannot.   
   >   
   > Does someone have a clearer picture about moving iterators?   
      
   I view move_iterator (and anything else that returns an rvalue reference   
   when dereferenced) as sort of a clever hack for getting more re-use out   
   of certain existing algorithms. They probably shouldn't be thought of   
   as a general tool or used liberally, but if you know enough about the   
   implementation of a given algorithm, you can use move_iterator to avoid   
   writing a very similar implementation for an algorithm that does   
   something different. For example, you can use std::copy with   
   move_iterator to produce an algorithm that moves elements from one range   
   into another.   
      
   HTH,   
      
   --   
   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)   
|