d更好的C在ctfe生成类型信息

原文
-betterCDMD编译:

// 工作
int f_1(int a){
    enum int[]foo = [1,2,3];
    if(__ctfe)
        return foo[1];
    return 1;
}

// 工作
int f_2(int a){
    static immutable int[] foo = [1,2,3];
    if(__ctfe)
        return foo[a];
    return 2;
}
 //失败,显示`TypeInfo`不能与`-betterC`一起用
int f_3(int a){
    enum int[]foo = [1,2,3];
    if(__ctfe)
        return foo[a]; // 在此发出错误
    return 3;
}

ldc,这3个函数都正常工作.
我发现如下解决方法:

int f_4(int a){
    enum int[]foo_ = [1,2,3];
    enum int[foo_.length]foo = foo_;
    if(__ctfe)
        return foo[a];
    return 4;
}
posted @ 2023-01-07 22:19  zjh6  阅读(16)  评论(0)    收藏  举报  来源