根据文档,readf
应该返回一个uint
.但即便是这个简单的例子也无法编译:
你好ð
import std.stdio;
void main() {
int x;
uint r = readf("%s", &x);
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
hello.d(5): Error: expression readf("%s",& x) is void and has no value
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么吗?
我正在使用dmd(Digital Mars D)编译器v2.050.
使用代码完成和简单重构等功能,在Mono-D中编写D几乎与在Visual Studio中编写C#一样高效.一切都在Linux上运行良好,只需安装Mono Develop,添加Mono-D存储库,然后构建; 但在Windows上我甚至无法编译Hello World.
起初似乎DMD找不到Phobos,但按照" 入门"页面上的说明操作后,我在Object.di文件中得到21个错误.错误如"找到'char'时期望')'"和"没有标识符声明者不可变".
我在Windows 7 64bit上使用Mono Develop 2.8.5.
任何人都知道如何让这个工作?
我正在尝试将D sort
函数作为函数的模板参数发送pipe
.当我使用sort
没有模板参数时,它工作:
import std.stdio,std.algorithm,std.functional;
void main()
{
auto arr=pipe!(sort)([1,3,2]);
writeln(arr);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用sort
模板参数时:
import std.stdio,std.algorithm,std.functional;
void main()
{
auto arr=pipe!(sort!"b<a")([1,3,2]);
writeln(arr);
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误 - main.d(5): Error: template instance sort!("b<a") sort!("b<a") does not match template declaration sort(alias less = "a < b",SwapStrategy ss = SwapStrategy.unstable,Range)
为什么会这样?sort!"b<a"
它自己工作,它有相同的参数和返回类型sort
,所以为什么pipe
接受sort
但不是sort!"b<a"
?我尝试做的是否有正确的语法?
UPDATE
好的,我试图包装该sort
功能.以下代码有效:
import std.stdio,std.algorithm,std.functional,std.array;
template mysort(string comparer)
{
auto mysort(T)(T source)
{
sort!comparer(source);
return source;
}
} …
Run Code Online (Sandbox Code Playgroud) 我在使用字符实例化RedBlackTree容器时遇到问题,但它可以使用ints:
import std.stdio;
import std.container;
void main()
{
auto r1 = redBlackTree!(int)(); // works
auto r2 = redBlackTree!(char)(); // error instantiating
}
Run Code Online (Sandbox Code Playgroud)
我正在使用DMD32 D Compiler v2.060.
有什么想法吗?谢谢.
我试图静态链接sqlite3但没有成功.我正在使用'etc.c.sqlite3'标题和sqlite3合并.为了创建.lib文件,我尝试了VC++和MinGW-gcc,这两个文件都成功编译了源文件 - 但它们都生成COFF对象格式(optlink,DMD使用,与OMF一起使用).在阅读了'digitalmars.D'上的大量帖子后,我尝试了几种不同的解决方案.
试图转换用GCC创建的lib文件,导致未定义的符号,如__divdi3和__muldi3,无法解决这个问题.
还尝试将sqlite3.o文件转换为*.obj,然后使用digitalmars'lib.exe' - 也不成功
在VC++生成的lib上尝试objconv失败,原因是:"SQLite.lib是一个导入库"
_sqlite3_open
,_sqlite3_errmsg
,_sqlite3_close
...如果我在VC++创建的库文件上使用coffimplib,程序会生成一个几乎为空的文件(~2KB),它只包含垃圾(即根本没有符号,大多数只是'null'值).
如果我对GCC创建的库执行相同操作,coffimplib会抱怨"不是导入库",并且不会生成转换后的库文件.
如果我使用DMC编译sqlite3合并,则编译失败会抱怨大量错误.所以我在这里,无处可去,有没有人有任何想法或提示可以解决这个问题?
注意:我不想使用DLL,但静态链接sqlite(对于可执行文件大小的问题).
string reverse(string str) pure nothrow
{
string reverse_impl(string temp, string str) pure nothrow
{
if (str.length == 0)
{
return temp;
}
else
{
return reverse_impl(str[0] ~ temp, str[1..$]);
}
}
return reverse_impl("", str);
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这段代码应该进行尾调用优化,但我不知道DMD是否正在这样做.哪个D编译器支持尾调用优化,他们会在这个函数上执行吗?
std.conv.to!string(enum.member)
工作怎么样?一个函数如何获取枚举成员并返回其名称?它是使用编译器扩展还是类似的东西?自从我来自C/C++世界以来,这对我来说有点平常.
不幸的是,我似乎无法在最小的工作示例中重现此行为,因此这可能过于模糊.但是,我至少可以说明不会导致这种行为的原因.
我有一个D模块,包含几个带有单元测试的类,其结构类似于:
import std.stdio;
class c1{
int c1func(int blah){ return blah; }
unittest{
writeln("Testing c1 func.");
stdout.flush();
}
}
class c2(T:c3) : c1{
private static int c2func(int blah){ return blah; }
unittest{
writeln("Testing c2 func.");
stdout.flush();
}
int c2fun2(int blah){
T tmp = new T();
return tmp.c3fun(blah);
}
}
class c3{
abstract int c3fun(int blah);
}
class c4 : c3{
override int c3fun(int blah){ return blah; }
}
unittest{
writeln("Testing class c1.");
stdout.flush();
c1 myc2 = new c2!(c4)(); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试与C++和D进行互操作.而我今天发现的东西真的让我大吃一惊:对象没有在我的程序中正确传递.
最好是展示一个例子.
我有一个C++库,我编译成一个目标文件和D程序,我与我的库链接并运行.
他们来了:
#include <stdio.h>
class Color
{
public:
Color(unsigned int _r, unsigned int _g, unsigned int _b) : r(_r), g(_g), b(_b) {}
unsigned int r, g, b;
};
class Printer
{
public:
Printer() {}
~Printer() {}
static Printer* getInstance();
void print(Color *c);
};
Printer* Printer::getInstance()
{
return new Printer();
}
void Printer::print(Color *c)
{
printf("(%d, %d, %d)\n", c->r, c->g, c->b);
}
Run Code Online (Sandbox Code Playgroud)
和D程序:
import std.stdio;
extern(C++)
{
class Color
{
uint r, g, b;
this(uint _r, uint _g, uint …
Run Code Online (Sandbox Code Playgroud) 我可以测试DMD正在使用编译给定代码version(DMD){}
,但是如何检查它的哪个版本?(2.66/2.65等)
更简洁地说,我想检查@nogc
修改器是否存在,如果不存在 - 定义一个虚拟修改器.
我提出了一个解决方法:
static if(!__traits(compiles, ()@nogc{}))
{
struct nogc;
}
Run Code Online (Sandbox Code Playgroud)
但有更好的方法吗?例如,甚至直接检查特定修饰符的存在?