Skip to content

Instantly share code, notes, and snippets.

@nialv7
Last active November 22, 2015 01:13
Show Gist options
  • Save nialv7/732fc682f4023744ed86 to your computer and use it in GitHub Desktop.
Save nialv7/732fc682f4023744ed86 to your computer and use it in GitHub Desktop.
import std.meta, std.typetuple, std.traits;
template ArrayTypeOf(int n, T...) {
static if (T.length == 0)
alias ArrayTypeOf = T;
else
alias ArrayTypeOf = AliasSeq!(T[0][n], ArrayTypeOf!(n, T[1..$]));
}
struct Transpose(T, int n) {
alias names = FieldNameTuple!T;
struct Accessor {
int index;
Transpose!(T, n) *ptr;
ref auto opDispatch(string name)() {
enum x = staticIndexOf!(name, names);
return ptr.store[x][index];
}
}
ArrayTypeOf!(n, Fields!T) store;
auto opIndex(int i){
return Accessor(i, &this);
}
}
struct A{
int a;
float b;
}
void main() {
Transpose!(A, 10) xx;
import std.stdio;
writeln(xx[2].a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment