error when std.range.Cycle of static array is a member

Lutger Blijdestijn lutger.blijdestijn at gmail.com
Sun Jul 24 07:51:32 PDT 2011


Jonathan M Davis wrote:

> On Sunday 24 July 2011 12:06:47 Lutger Blijdestijn wrote:
>> I'm trying to have a Cycle range of a static array as a struct member,
>> but getting a compilation error. Can anybody tell me if I'm doing
>> something wrong, or is this a bug?
>> 
>> import std.range;
>> 
>> struct Foo
>> {
>>     ubyte[] buf;
>>     Cycle!(typeof(buf)) cbuf1; // ok
>> 
>>     ubyte[1024] staticBuf;
>>     Cycle!(typeof(staticBuf)) cbuf2; // error
>> 
>>     void test()
>>     {
>>         Cycle!(typeof(staticBuf)) cbuf3;
>>         cbuf3 = cycle(staticBuf); // ok
>>     }
>> }
>> 
>> /std/range.d(228): Error: template instance
>> std.array.front!(ubyte[1024u]) incompatible arguments for template
>> instantiation
> 
> static arrays are not ranges. You can't pop the front off of them, so no
> range- based algorithm would work with a static range. Now, you _can_ get
> a dynamic range over a static range and _that_ is a valid range, so what
> you need to do is make the Cycle a Cycle!(ubyte[]) rather than
> Cycle!(ubyte[1024]), and you when you pass the static array to cycle, you
> need to slice it: cycle(staticBuf[]).
> 
> - Jonathan M Davis

That would work, but the docs explicitly mention that Cycle is specialized 
for static arrays (for performance reasons). This is how cycle is 
implemented:

Cycle!(R) cycle(R)(ref R input, size_t index = 0) if (isStaticArray!R)
{
    return Cycle!(R)(input, index);
}


More information about the Digitalmars-d-learn mailing list