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

Eduardo Pinho via Digitalmars-d digitalmars-d at puremagic.com
Thu Feb 5 02:36:33 PST 2015


Good day. I came across something strange in the Phobos library, 
and would like to understand whether this is an issue in the 
implementation or something that I overlooked. I have kept the 
question in Stack Overflow for a while (still unanswered), but I 
will put the essentials in this post as well. 
http://stackoverflow.com/questions/28304856/should-std-algorithm-find-demand-a-reference-to-range-elements

I was implementing a class-based finite random access range, and 
wished to test it with functions in the algorithm library. Once I 
came into `std.algorithm.find` (the find_if variant), a 
compilation error popped up: "algorithm.d|4838|error: foreach: 
cannot make e ref"

This was in GDC 4.9.2, but the same snippet can be found in the 
repository here: 
https://github.com/D-Programming-Language/phobos/blob/master/std/algorithm/searching.d#L1492

I actually understand why this happened, since I did not provide 
either range properties returning by reference or `opApply` 
taking a delegate with a `ref` argument. The thing is that I feel 
that I should not be obliged to provide them at all. It is not a 
requirement for an input range to provide elements by reference, 
and find_if does not seem to need them either. I can change the 
`foreach` to take elements by value, and my tests would work just 
fine.

Some assistance on understanding what is wrong here is greatly 
appreciated.


More information about the Digitalmars-d mailing list