Prev: Better way to call member function using function pointer?
Next: Trying to come to terms with typecasting operators
From: Roman.Perepelitsa on 20 Nov 2009 04:10 On 11 Nov, 06:20, Andrei Alexandrescu <SeeWebsiteForEm...(a)erdani.org> wrote: > I wrote an article that may be of interest on InformIt; it got mentioned > on reddit.com. Due to the vagaries of posting time and reddit dynamics, > the article didn't stay long on reddit's programming page so it got very > few views and comments. Great article. A step-by-step introduction to the new concepts makes it very easy to follow. I have one question regarding infinite RandomAccessRange concept. It has method RandomAccessRange slice(int i, int j); So it'll produce a finite range, right? Why there is no way to produce an infinite subrange in constant time? Roman Perepelitsa. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: itaj sherman on 6 Dec 2009 03:18
On Nov 11, 7:20 am, Andrei Alexandrescu <SeeWebsiteForEm...(a)erdani.org> wrote: > I wrote an article that may be of interest on InformIt; it got mentioned > on reddit.com. Due to the vagaries of posting time and reddit dynamics, > the article didn't stay long on reddit's programming page so it got very > few views and comments. > > http://www.reddit.com/r/programming/comments/a2hv3/ > > Please comment here and/or there and vote up if you liked it. > > Andrei > This is a very intersting approach to learn and think about ranges. I always thought about ranges as a separate concept from iterators, now I get that range is actually a more ellaborate type of iterator. Some kind of a bounded iterator. It makes very clear why they are useful as a concept and in practical code. I understood the reasons in which you suggested ranges as a replacement for pairs of simple iterators, in situations such as *iterating* through a container :), and in parameters for algorythms like sort and reverse. What is not exactly clear to me is why you suggest to abandon simple iterators altogether. The fact that there are good situations to use ranges instead of the traditional iterators, doens't mean that all situations are so. For example, algorythms like std::copy, should recieve such a ForwardRange as the source to copy from, but a ForwardIterator as the target (possibly of different container, possibly an input iterator). I suppose if you pass a range as the target parameter, it could behave as an iterator with boundary (possibly boundary check). In this sense (but maybe not other senses), a range is a type of iterator, that also has boundary. Going further with this notion of "ranges are very important, but also are not everything": In the same way as using ranges as an abstraction to get the described situations better than with iterators, there are situation which might benefit from even further abstractions. For example, if you have a range and you want to be able to separate it on a certain place into two parts (I'll call it a "Cut"), then you can have an object to represent that Cut: { .... ForwardRange myRange = myContainer.getFullRange(); ForwardIterator myMidLocation = find( MyRange, some_predicate ); ForwardCut myCut( myRange, myMidLocation ); //define an object for the cut auto myCut = cut_range( myRange, myMidLocation ); // or like this //just to help explain the structures: std::cout << "The range of all elements in my container: " << myRange; //implemented to list the values of the elements std::cout << "I separate the container to two parts: "; std::cout << "[ " << myCut.front() << ", " << myCut.mid() << ") "; std::cout << "[ " << myCut.mid() << ", " << myCut.back() << ") "; //but my real point is that then you can write: rotate( myCut ); // it could all be done together to illustrate: rotate( cut_range( myRange, find( myRange, some_predicate ) ) ); } I think that the parameter of rotate() could be a single Cut object, rather than 3 iterators or 2 ranges or a range and an iterator. The return value of rotate is a Cut with the same begin and end iterators but different mid. Arguably rotate could just return the new mid iterator. What I'm trying to say, in another way, is that bounded iterators (ranges) are, as you say, useful in many cases. Specifically in some of the cases that traditionaly simple iterators were used. What your article got me to understand is that a range is a special type of iterator, a bounded iterator, and not just another separate concept. But, I don't see why that more elaborate type of iterator, should totaly elliminate the simple one. In the same way, there may be even more types such as double-bounded iterators (cuts). itaj -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |