Range with an alias parameter

Alex sascha.orlov at gmail.com
Wed Jan 30 20:56:00 UTC 2019


On Wednesday, 30 January 2019 at 20:13:56 UTC, Alex wrote:
> Given this:
>
> ´´´
> import std.experimental.all;
>
> void main(){}
>
> static assert(isInputRange!(ReturnType!(produceS!(42))[]));
>
> auto produceS(size_t param)() { return S!param(); }
> struct S(size_t param)
> {
>     //@disable this(this);
>     auto opIndex() { return produceRange!(this); }
> }
>
> auto produceRange(alias source)(){ return Range!source(); }
>
> struct Range(alias source)
> {
>     size_t front();
>     void popFront();
>     bool empty();
> }
> ´´´
>
> Why disabling copying of S removes the range property of Range?

Ok... strange... it doesn't in fact... as this works:

´´´
import std.experimental.all;

void main()
{
     S!42 s;
     auto res = s[];
     static assert(isInputRange!(typeof(res)));
     res.front.writeln;
}

//static assert(isInputRange!(ReturnType!(produceS!(42))[]));

auto produceS(size_t param)() { return S!param(); }
struct S(size_t param)
{
     auto myParam(){return param; }
     @disable this(this);
     auto opIndex() { return produceRange!(this); }
}

auto produceRange(alias source)(){ return Range!source(); }

struct Range(alias source)
{
     size_t front(){return source.myParam;}
     void popFront();
     bool empty();
}
´´´


More information about the Digitalmars-d-learn mailing list