语: how to enforce an interface on a template function

标签 d

我已经在谷歌上搜索了两个小时,但没有成功。

如果我有一个模板函数并且我想在模板类型上强制执行一个接口(interface),我该怎么做?

例如。

void doStuff(T)(bool param) /*if T is a Throwable*/ {
    // do stuff...
}

最佳答案

您可以使用模板约束来做到这一点:

void doStuff(T)(bool param) if(is(T : Throwable)) { }

is 表达式可能非常复杂,所以我建议通读 this .

关于语: how to enforce an interface on a template function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41293536/

相关文章:

d - 如何打印inout参数?

dlang : relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC

d - 具有不可复制类型的数组

gdb - D2和gdb问题

algorithm - 为什么这些小型 D 程序的行为不同?

macos - 无法在 OS X 10.6.8 上安装 DMD 2.060

enums - 根据成员的值将 int 转换为适当的枚举值

compiler-errors - 为什么会出现模块冲突?

class - 访问外部类

windows - D 运行时作为 DLL