dlang语言以下代码import stdstdio;void main string s = hello; const ulong d = slength; intd arr = 12345; writelnarr;编译出错a_变量作为数组容量去定义a_变量作为数组容量去定义d6 Error variable s cannot be read at compile tim
- 修改方式一:使用数组字面量时直接使用s.length作为数组大小
import std.stdio;
void main() { string s = "hello"; int[s.length] arr = [1,2,3,4,5]; writeln(arr); }
- 修改方式二:将d定义为编译时常量并使用
import std.stdio;
void main() { string s = "hello"; enum d = s.length; int[d] arr = [1,2,3,4,5]; writeln(arr); }
- 修改方式三:将d定义为运行时变量并使用cast转换类型
import std.stdio;
void main() { string s = "hello"; ulong d = s.length; int[cast(size_t)d] arr = [1,2,3,4,5]; writeln(arr); }
这三种方式都可以解决该错误,具体选择哪种方式可以根据实际需求和代码风格进行选择
原文地址: https://www.cveoy.top/t/topic/fFsf 著作权归作者所有。请勿转载和采集!