标签: dmd

如何轻松初始化函数指针?

我想使用Runtime.loadLibrary和加载Win32 API函数GetProcAddress(...).使用mixin:

template GetProcA(alias func, alias name_in_DLL)
{
    const char[] GetProcA = func ~ ` = cast(typeof(`~func~`)) GetProcAddress(hModule,"`~name_in_DLL~`");`;
}
...
static extern (Windows) Object function (HWND hWnd, int nIndex) GetWindowLong;
static extern (Windows) Object function (HWND hWnd, int nIndex, Object dwNewLong) SetWindowLong;
Run Code Online (Sandbox Code Playgroud)

我可以通过这种方式实例化它(在类构造函数中):

mixin GetProcA!("SetWindowLong", "SetWindowLongA");
Run Code Online (Sandbox Code Playgroud)

但如果再次使用它为另一个功能:

mixin GetProcA!("GetWindowLong", "GetWindowLongA");
Run Code Online (Sandbox Code Playgroud)

编译器抱怨:

mixin GetProcA!("GetWindowLong","GetWindowLongA") GetProcA isn't a template...
Run Code Online (Sandbox Code Playgroud)

我没有看到这一点:如果创建了第一个实例GetProcA,我又不能再使用它,那么它对我有什么帮助呢?

d dmd

4
推荐指数
1
解决办法
363
查看次数

如何将ref/out函数指针传递给函数?

我想实例化一个函数指针:

static void GetProc (out function f) {
    auto full = demangle(f.mangleof);
    auto name = full[full.lastIndexOf('.')+1..$];

    f = cast(typeof(f)) GetProcAddress(hModule,name.toStringz);
}
Run Code Online (Sandbox Code Playgroud)

但编译器不会让我使用函数类型变量(out function f).我试过用Object但显然function不是Object(怎么回事?).那么,我如何传递functionas ref/ outvariable(不使用template/ mixin,它会掩盖代码并迫使我添加许多typeof语句......)?

d dmd

3
推荐指数
1
解决办法
158
查看次数

使用Derelict和OpenGL时"分段错误:11"

我试图在OSX 10.7上运行它:

/**
 * simple.d
 */

import std.stdio;
import derelict.sdl.sdl;
import derelict.sdl.macinit.SDLMain;
import derelict.opengl.gl;

pragma(lib, "/usr/local/src/Derelict2/lib/libDerelictUtil.a");
pragma(lib, "/usr/local/src/Derelict2/lib/libDerelictSDL.a");
pragma(lib, "/usr/local/src/Derelict2/lib/libDerelictGL.a");

int main(string[] args) {

    // Load Derelict
    writeln("Loading SDL...");
    DerelictSDL.load();

    // Initialise SDL
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) {
        throw new Exception("SDL initialization failed");
    }

    // Enable Double Buffering
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    // Set up the screen 
    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
    if (screen == null) {
        throw new Exception("Screen is null");
    }
    SDL_WM_SetCaption("Simple", "Simple");

    // Print …
Run Code Online (Sandbox Code Playgroud)

opengl macos sdl d dmd

3
推荐指数
1
解决办法
875
查看次数

无法覆盖结构的opCmp和opEquals

来自D的文档:

或者,您可以使用auto ref参数声明单个模板化opEquals函数:

bool opEquals()(auto ref S s) { ... }
Run Code Online (Sandbox Code Playgroud)

<...>

如果struct声明了一个opCmp成员函数,它应该遵循以下形式:

int opCmp(ref const S s) const { ... }
Run Code Online (Sandbox Code Playgroud)

为什么以下代码无法编译呢?

import std.stdio;
import std.conv;

struct Fgs {
    int v;
    this(int iv) {
        v = iv;
    }

    bool opEquals()(auto ref Fgs another) {
        return v == another.v;
    }

    int opCmp(ref const Fgs another) const {
        if (this == another) {
            return 0;
        } else if (this.v < another.v) {
            return -1;
        } else {
            return 1; …
Run Code Online (Sandbox Code Playgroud)

d dmd

3
推荐指数
1
解决办法
298
查看次数

OSX上的DMD问题

我试图按照本页面提供的建议精确安装DMD:http://dlang.org/dmd-osx.html.但是,当我尝试生成.d文件时出现错误:

这是我的hello.d

import std.stdio;

void main() {
    writeln("hello world!");
}
Run Code Online (Sandbox Code Playgroud)

然后:

gcc hello.o -o hello -m64 -lphobos2 -lpthread -lm
ld: library not found for -lphobos2
clang: error: linker command failed with exit code 1 (use -v to see invocation)
--- errorlevel 1
Run Code Online (Sandbox Code Playgroud)

有没有人遇到过同样的问题?

谢谢,

塞德里克

macos d ld dmd

3
推荐指数
1
解决办法
221
查看次数

将D字符串转换为C char*

好吧,基本上这就是我需要的:

  • 我有一个extern(al)char *变量
  • 我想分配D字符串的值

代码:

import std.stdio;
import std.string;
import core.stdc.stdlib;

extern (C) int yyparse();
extern (C) extern __gshared FILE* yyin;
extern (C) extern __gshared char* yyfilename;

void main(string[] args)
{
    string filename = args[1];

    auto file = File(filename, "r");

    yyfilename = toStringz(filename);
    yyin = file.getFP();

    yyparse();
}
Run Code Online (Sandbox Code Playgroud)

但是,该toStringz函数返回此错误:

main.d(15): Error: cannot implicitly convert expression (toStringz(filename)) of type immutable(char)* to char*
Run Code Online (Sandbox Code Playgroud)

知道出了什么问题吗?

c c++ d dmd

3
推荐指数
2
解决办法
306
查看次数

使用foreach循环 - 无法读取变量

应该相当简单,但事实并非如此.

这是我的代码:

string cases()
{
    string ret = "";
    string[] methods;

    methods = [__traits(derivedMembers,mixin("Math"))];
    foreach (string s; methods) ret ~= "case \"" ~ s ~ "\": return Math."~s~"(params);";

    methods = [__traits(derivedMembers,mixin("OtherClass"))];
    foreach (string s; methods) ret ~= "case \"" ~ s ~ "\": return OtherClass."~s~"(params);";

    return ret;
}

string execute(string what, string[] params)
{
    switch (what)
    {
        mixin(cases());
        default: break;
    }
    return "";
}
Run Code Online (Sandbox Code Playgroud)

我想做的事 :

const string[] arrayWithClassNames = ["Math","SomeClass"];
foreach (string s; arrayWithClassNames)
{
     methods = ...
     foreach …
Run Code Online (Sandbox Code Playgroud)

d mixins dmd

3
推荐指数
1
解决办法
333
查看次数

在编译时D任意代码

我听说D可以在编译时执行任意用户代码.这是真的?有人可以给出一个例子(特别是当编译永远不会终止时)?这个功能是为了什么?另外,如果是这样的话有一种方法可以通过一些编译器选项来禁用它(编译器是dmd)?

d dmd ctfe

3
推荐指数
1
解决办法
128
查看次数

在D中获取课程的家长

如何从该类外部获取类的基类型?我parent在文档中找到了这个特性,但我不确定如何使用它.

module test;
import std.stdio;

class A {}
class B : A { }

void main() {
    writeln(typeid(__traits(parent, B)));
    writeln(typeid(__traits(parent, test.B))); // try prefixing with module name
}
Run Code Online (Sandbox Code Playgroud)

我希望至少在第二种情况下获得A,但这会打印出来

void
void
Run Code Online (Sandbox Code Playgroud)

理想情况下,我想将某个类T的基类分配给别名,并像使用任何其他类型一样使用它.例如:

alias T = __traits(parent, V);
T t = new T();
Run Code Online (Sandbox Code Playgroud)

d dmd

3
推荐指数
1
解决办法
99
查看次数

初始化表达式是D中构造函数的一部分吗?

在D中,我可以直接在声明上初始化并期望初始化表达式是构造函数的一部分吗?我来自C#,就是这样.但随着DMD 2.071.0我得到其他行为.

class Other { }

class Test { Other nonStaticMember = new Other; }

void test()
{
    auto t1 = new Test;
    auto t2 = new Test;
    // Assert is failing, the two Test instances are 
    // being initialized to the same reference
    // instead of execute the Other constructor twice.
    assert(t1.nonStaticMember !is t2.nonStaticMember);
}
Run Code Online (Sandbox Code Playgroud)

如果这是意图行为,应在此处记录:https://dlang.org/spec/class.html 对吗?

d class initializer dmd

3
推荐指数
1
解决办法
88
查看次数

标签 统计

d ×10

dmd ×10

macos ×2

c ×1

c++ ×1

class ×1

ctfe ×1

initializer ×1

ld ×1

mixins ×1

opengl ×1

sdl ×1