d枚举当索引

枚举当索引

enum Foo {
    one,
    two
}

struct struct_t {}

struct array {
    static private struct_t[Foo.max + 1] content;
    static struct_t opIndex(Foo idx) { return content[cast(int) idx]; }     
    //重载.
}

void main() {
    struct_t a = array[Foo.one];
}

然后,如果想要更简单的重用,可以将其泛化.


struct enum_array(Key, Value) {
        static private struct_t[Key.max + 1] content;
        static Value opIndex(Key idx) { return content[cast(int) idx]; }
}

alias array = enum_array!(Foo, struct_t);
posted @ 2022-10-17 10:04  zjh6  阅读(10)  评论(0)    收藏  举报  来源