d编译时与运行时关联数组

enum string[string] CtfeFoo = ["foo" : "bar"];
static immutable string[string] Foo;

static this()
{
    Foo = CtfeFoo;
}

string ctfeableFunction()
{
    if (__ctfe)
    	return CtfeFoo["foo"];
    else
        return Foo["foo"];
}

void main()
{
    enum a = ctfeableFunction();
    auto b = ctfeableFunction();
}
发布了346 篇原创文章 · 获赞 25 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/fqbqrr/article/details/104036170