RFC on range design for D2

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Sep 9 11:36:33 PDT 2008


Sean Kelly wrote:
> Andrei Alexandrescu wrote:
>>
>> Then there remains the problem:
>>
>> void bump(R, V)(R r)
>> {
>>     for (; !r.isEmpty; r.next) ++r.first;
>> }
>>
>> bump(charArraySucker); // bogus
>>
>> Sigh...
> 
> And I suppose you don't want to return a const reference from first() 
> because the user may want to operate on the value, if only on a 
> temporary basis?  Let's say:
> 
> P findFirst(P, R, C)( R r, C c )
> {
>     for( ; !r.isEmpty; r.next )
>     {
>         // modify the temporary because a new element is expensive
>         // to copy-construct
>         if( auto p = c.contains( ++r.first ) )
>             return p;
>     }
>     return P.init;
> }
> 
> Hm... must the implementation prevent stupid mistakes such as your 
> example?  Ideally, yes.  But I don't see a way to do so and yet allow 
> for routines like findFirst() above.

Yes, exactly. Also consider a user that inspects lines in a file, and 
occasionally takes ownership of the current line to put it into a 
hashtable or something.

I think I'll resign myself to isEmpty/next/first for input ranges. The 
remaining difference between input ranges and forward ranges is that the 
former are uncopyable.


Andrei


More information about the Digitalmars-d-announce mailing list