How templates work (bonus) - Full instantiation of Iota!(1,5)

Simen Kjærås simen.kjaras at gmail.com
Thu Jun 4 13:37:36 UTC 2020


On Thursday, 4 June 2020 at 13:21:08 UTC, Stanislav Blinov wrote:
> On Thursday, 4 June 2020 at 11:33:48 UTC, Stefan Koch wrote:
>> As part of my series of templates
>> I have done a a full instantiation of Iota!(1,5).
>> (By hand!)
>> And here is the result:
>>
>> // ...
>>
>> Because it has been done manually there are probably some bugs.
>> Can you find them all?
>
> Is that really full? I.e. you're not counting the 
> instantiations of Seq on purpose? :)
>
> If I understand correctly, the below (basically manually 
> written version of std.meta.aliasSeqOf) would just be Iota!(1, 
> 5) => Iota?
>
> template Iota(size_t first, size_t last) {
>     struct Result {
>         static foreach (i; first .. last)
>             mixin("auto e", i, " = ", i, ";");
>     }
>     enum Iota = Result.init.tupleof;
> }

That is a cool (but pretty darn ugly) trick. Of course, it uses 
CTFE, so there's a whole nother set of complications that we 
haven't yet covered. But if we ignore those the instantiation 
graph would be this:

Iota!(1, 5):
{
     enum Iota = (1, 2, 3, 4);
}.Iota = (1, 2, 3, 4)

--
   Simen


More information about the Digitalmars-d mailing list