我非常了解静态构造函数,但是static this()在类之外有什么意义呢?
import std.stdio;
static this(){
int x = 0;
}
int main(){
writeln(x); // error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我如何访问定义的变量static this()?
D是编译速度最快的编程语言之一,如果不是最快的话,但情况并非总是如此.unittest打开时,事情变得非常缓慢.我目前的项目有6-7个模块(~2000 LOC),每个模块都有单元测试,也包含基准测试.以下是我当前项目中的一些数字:
dmd -O -noboundscheck 需要 0m1.287s
dmd -O -release -noboundscheck 需要 0m1.382s
dmd -O -inline -noboundscheck 需要 0m1.499s
dmd -O -inline -release -noboundscheck 需要 0m3.477s
添加-unittest到上述任何一个将大大增加编译时间:
dmd -O -inline -release -noboundscheck -unittest 需要 0m21.918s
有时会崩溃DMD:
time dmd -O t1.d -inline -noboundscheck -version=Double -unittest 需要 0m2.297s
Internal error: ../ztc/gdag.c 776
显然,unittest是错误的,但同时它已成为我项目的重要组成部分.我想知道减速是否正常还是正在进行的工作?我的项目正在不断发展,每一个新的单元测试都需要更长时间的编译.我知道的唯一解决方案是禁用-release和-inline,但这并不总是可取的.
我听说在DMD 2.058中会有一个新的匿名函数语法,但是我找不到任何关于它的信息.什么是新语法,旧语法将被弃用?
这有效:
int[] a = [ 1, 2, 3, 4 ];
fill(a, 5);
Run Code Online (Sandbox Code Playgroud)
但这不是:
int[4] a = [ 1, 2, 3, 4 ];
fill(a, 5);
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
错误:模板std.algorithm.fill(Range,Value)if(isForwardRange!(Range)&&是(typeof(range.front = filler)))与任何函数模板声明都不匹配
相反,我必须这样做才能使它与静态数组一起使用:
int[4] a = [ 1, 2, 3, 4 ];
fill(a[], 5);
Run Code Online (Sandbox Code Playgroud)
请问任何人解释这种行为吗?
编译时间功能评估(CTFE)如何工作?我试图理解编译器在运行时如何创建不存在的东西(例如,函数)并执行它.我习惯于通过编译将源代码变成二进制文件,然后执行二进制文件.那么,源代码如何在编译器运行时成为可执行的东西并且能够运行它?函数是否真的被创建并运行,或者它只是函数调用的模拟?
今天的显示器显示器每通道8位或24位色,大多数都以sRGB色彩模式运行.GUI和图形库(如Qt和X)在这些限制范围内运行.例如,您可以从无符号字符数组(每通道8位)创建一个QImage,但不能再创建.
那么那些提供1024种灰度的高端显示器会发生什么呢?Qt不提供30位彩色模式,X也不提供.每个通道如何利用所有位?
struct M{
T opIndex(uint i){ ... }
}
Run Code Online (Sandbox Code Playgroud)
这给了我这个:
m[i]
Run Code Online (Sandbox Code Playgroud)
但是,如果我想要它在二维中,那么我可以这样做:
m[i][j]
Run Code Online (Sandbox Code Playgroud)
无论如何要做到这一点?
Tuple和TypeTuple有什么区别?我查看了库中的示例代码,但它们看起来很相似.我该如何决定使用哪个?有没有一个很好的理由为什么Tuple在std.typecons但TypeTuple在std.typetuple?
import std.stdio;
void main(){
int n;
while(readf("%d", &n)){
if(n == 11)
break;
writeln(n);
}
}
Run Code Online (Sandbox Code Playgroud)
第一次迭代工作,并打印n,但之后readf()永远不会返回.
该文档只有一行解释readf():
uint readf(A ...)(以char []格式,A args);
Run Code Online (Sandbox Code Playgroud)Formatted read one line from stdin.
我做错了吗?或者有什么问题readf()吗?我只需要从标准输入中读取数字.
使用:DMD 2.054 64位
如果你需要在D中重写以下C++代码,你会怎么做?
struct A{
const S* _s;
B _b;
C _c;
mutable C _c1, _c2;
A(const B& b, const C& c, const S* s){ /*...*/ }
void compute(const R& r) const
{
//...
_c1 = ...
_c2 = ...
}
};
Run Code Online (Sandbox Code Playgroud)
D没有mutable,而且根据我的经验,它很少用于C++.但是,假设mutable在这里使用正确的原因,我在D中的选择是什么?