Auto recursive function

Bauss via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 12 00:29:37 PST 2017


On Thursday, 12 January 2017 at 00:30:33 UTC, Ignacious wrote:
> On Wednesday, 11 January 2017 at 19:23:10 UTC, Razvan Nitu 
> wrote:
>> [...]
>
>
> If you change the return type to a void* your code basically 
> works.
>
>
> void* makeMultidimensionalArray(T, Allocator)(auto ref 
> Allocator alloc, size_t[] lengths)
> {
>     if (lengths.length == 1)
>     {
> 		int x = 0x01FEEF01;
>         return cast(void*)makeArray!T(alloc, lengths[0], x);
>     }
>     else
>     {
>         alias E = typeof(makeMultidimensionalArray!T(alloc, 
> lengths[1..$]));
>
>         auto ret = makeArray!E(alloc, lengths[0]);
>         foreach (ref e; ret)
>             e = makeMultidimensionalArray!T(alloc, 
> lengths[1..$]);
>         return cast(void*)ret;
>     }
> }
>
> The problem is that then you need to cast back and that 
> essentially results in the original problem. Can be done but 
> probably gonna have to use string mixins.

That isn't a solution and will probably introduce other bugs.

As soon as you have to result to using void* is an indication of 
bad design IMO, because it leads to very unsafe possibilities.


More information about the Digitalmars-d-learn mailing list