Void pointers
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon May 16 14:41:20 PDT 2016
On 5/16/16 5:38 PM, Alex wrote:
> 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...
Hey, there's nothing wrong with for-loops. Just trying to answer the
question :)
You could also do something like:
foreach(i; 0 .. b.length) writeln(&b[i]);
-Steve
More information about the Digitalmars-d-learn
mailing list