Last active
November 22, 2015 01:13
-
-
Save nialv7/732fc682f4023744ed86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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