assuaging static array literals

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Mar 15 11:20:57 PDT 2010


I was thinking that two functions may help with static arrays of 
statically-known data:

import std.conv, std.stdio, std.traits;

auto staticArrayRef(Args...)() {
     static string expand() {
         string result = "Args[0]";
         foreach (i; 1 .. Args.length) {
             result ~= ", Args[" ~ to!string(i) ~ "]";
         }
         return result;
     }
     enum size_t len = Args.length;
     static CommonType!(Args)[len] result = mixin("["~expand()~"]");
     return result;
}

void main() {
     auto a = staticArrayRef!(1, 2, 3, 4.5)();
     writeln(typeof(a).stringof);
     writeln(a);
}

Essentially staticArrayRef returns a reference to a 
statically-initialized array with statically-known data.


Andrei



More information about the Digitalmars-d mailing list