d编译时扩展ctfe

原文
对:

//main.d
import std;

void main()
{
    enum x = iota(1, 5).reduce!"a * b";
    x.writeln();
}

通过类似:

dmd --expand-ctfe main.d -o expanded.d
cat expanded.d

得到:

//expanded.d
import std;

void main()
{
    24.writeln();
}

可试试-vcg-ast标志:

import object;
import std;
void main()
{
    enum int x = 24;
    writeln(24);
    return 0;
}

// 一堆模板实例

或者这样:

template printEnum(alias x, string file = __FILE__, size_t line = __LINE__)
{
   enum printEnum = x;
   pragma(msg, file, "(", int(line), "): ", printEnum);//打印.
}
enum x = printEnum!(iota(1, 5).reduce!"a * b");

可这样:

debug template printEnum(...) {
  ... // 上面内容仅用于调试时.
}//加上-debug标志.
else
enum printEnum(alias x) = x;

参考
大内存,用64位编译:

dmd -m64 -run test.d
posted @   zjh6  阅读(9)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示