March 15, 2010 assuaging static array literals | ||||
|---|---|---|---|---|
| ||||
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
| ||||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply