Should std.algorithm.find request a reference to range elements?

I am working on a finite class based random access range. When running multiple tests on it:

auto myRange = /* construct my range */
static assert (isRandomAccessRange!(typeof(myRange))); // 
static assert (!isInfinite!(typeof(myRange)));         // both pass 
auto preamble = myRange[0..128];
assert( all!"a == 0"(preamble)); // check for all zeros

I got this compilation error in GDC 4.9.2 regarding the last line in the above snippet: "algorithm.d | 4838 | error: foreach: cannot do e ref"

The error points to this piece of code in std.algorithm.find(find_if variant, taking a range and predicate), which actually refers to each element with foreach:

InputRange find(alias pred, InputRange)(InputRange haystack)
if (isInputRange!InputRange)
{
    alias R = InputRange;
    alias predFun = unaryFun!pred;
    static if (isNarrowString!R)
    {
        ...
    }
    else static if (!isInfinite!R && hasSlicing!R && is(typeof(haystack[cast(size_t)0 .. $])))
    {
        size_t i = 0;
        foreach (ref e; haystack) // <-- needs a ref
        {
            if (predFun(e))
                return haystack[i .. $];
            ++i;
        }
        return haystack[$ .. $];
    }
    else
    {
       ...
    }
}

This is most likely because I presented an implementation opApplythat does not provide an argument ref(nor does the class provide a return type reffor any other member function).

int opApply(int delegate(E) f) {...}
int opApply(int delegate(size_t,E) f) {...}

, , , foreach . :

. foreach , :

:

  • .empty true,
  • .front

:

  • .popFront()

( ), . , :

struct class, , foreach - opApply, foreach_reverse opApplyReverse. :

int opApply(int delegate(ref Type [, ...]) dg);

, .

std.algorithm.all, , , :

bool all(Range)(Range range) if (isInputRange!Range && is(typeof(unaryFun!pred(range.front))));

true , v, , . ( ) Ο (range.length) .

, Phobos, std.algorithm.find ? -, ?

+4
1

opApply , , , foreach, opApply. , opApply front, popFront empty, . opApply, opApply ref, front - . , front ref foreach, ref, opApply . , ref , , opApply, , opApply ref front.

, , , , , opApply , . , , , opApply . .

, Phobos, , (, ), save haystack, . , , , , , , , . , opApply / , std.algorithm.find , , .

EDIT:

. . . , opApply, , , - , opApply , opApply foreach, ( opApply, , ). , , , opApply ( , ).

, , - . opApply ref, ref, , , . , opApply , - , , , , , opApply, , , .

+1

All Articles