Void pointers

Alex via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 16 14:38:43 PDT 2016


On Monday, 16 May 2016 at 21:15:16 UTC, Steven Schveighoffer 
wrote:
> On 5/16/16 4:39 PM, Alex wrote:
>>      // something that does not worked as expected:
>>      // how to rewrite a for loop
>>      for(auto i = 0; i < b.length; i++) writeln(&b[i]);
>>      // into a foreach loop?
>>
>
> What you need is a range that produces void * instead element 
> itself.
>
> This would probably work:
>
> struct VoidRange
> {
>    void[] arr;
>    auto front() { return arr.ptr; }
>    void popFront() { arr.popFront; }
>    bool empty() { return arr.empty; }
> }
>
> foreach(p; VoidRange(b)) writeln(p);
>
> -Steve

Yes... I could do this... but then, I would construct millions of 
structs to hold just ordinal numbers... Using a iota would also 
be a possibility... but I want something even less perceptible...


More information about the Digitalmars-d-learn mailing list