Should std.algorithm.find demand a reference to range elements?
Jakob Ovrum via Digitalmars-d
digitalmars-d at puremagic.com
Thu Feb 5 02:55:16 PST 2015
On Thursday, 5 February 2015 at 10:36:34 UTC, Eduardo Pinho wrote:
> Some assistance on understanding what is wrong here is greatly
> appreciated.
There's definitely nothing wrong with your code.
I always thought foreach preferred opApply when available because
internal iteration can in theory be faster than external
iteration (although it rarely is with current compilers because
the indirect call to the delegate is often the bottleneck), but
as you've pointed out, the specification claims external
iteration is be preferred. Either the specification has to be
changed, or the compiler has to be changed.
If internal iteration remains preferred, `find` should probably
be changed to use an explicit for-loop:
---
size_t i = 0;
for (auto h = haystack.save; !h.empty; h.popFront())
{
if (predFun(h.front))
return haystack[i .. $];
++i;
}
return haystack[$ .. $];
---
(`haystack` is known to be a forward range in this portion of the
code)
However, this would set a possibly disruptive precedent that
range algorithms must no longer use foreach on aggregate types...
More information about the Digitalmars-d
mailing list