input range with non copyable element

vit vit at vit.vit
Mon Aug 9 11:03:27 UTC 2021


On Monday, 9 August 2021 at 02:47:40 UTC, Mathias LANG wrote:
> On Sunday, 8 August 2021 at 18:36:02 UTC, vit wrote:
>> Hello, is there reason why elements of input range must be 
>> copyable?
>
> By design, not that I can think of. But it is assumed all over 
> the place, unfortunately. You can make your `front` method 
> return by `ref`, but you're still going to get bitten as soon 
> as you do any `std.algorithm`-based operation, as storage 
> classes are not inferred 
> (https://issues.dlang.org/show_bug.cgi?id=9423) and things get 
> passed by value to your delegates by default. The problem can 
> also show up when you `foreach` over a range 
> (https://issues.dlang.org/show_bug.cgi?id=15413). And finally, 
> `std.algorithm` need to support it.
>
> So currently you'll have to jump through a lot of hops to get 
> it to work. It'll be much easier to use your own `map` & co to 
> get the job done for the time being. Hopefully at some point in 
> the near future that won't be needed anymore.

Look like I am not the first who has this problem: 
https://issues.dlang.org/show_bug.cgi?id=14478


I copy part of `std.algorithm` I use and change `isInputRange` to:
```d
enum bool isInputRange(R) = true
     && is(typeof(R.init) == R)									///no change
     && is(ReturnType!((R r) => r.empty) == bool)				///no change
	&& is(typeof((return ref R r) => (auto ref 
x){}(r.front)))	///Before: , is(typeof((return ref R r) => 
r.front))
     && !is(ReturnType!((R r) => r.front) == void)				///no change
     && is(typeof((R r) => r.popFront));							///no change



More information about the Digitalmars-d-learn mailing list