作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
来自 D 的 documentation :
Alternatively you can declare a single templated opEquals function with an auto ref parameter:
bool opEquals()(auto ref S s) { ... }
<...>
If structs declare an opCmp member function, it should follow the following form:
int opCmp(ref const S s) const { ... }
为什么下面的代码编译失败?
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;
}
}
}
unittest {
auto a = Fgs(42);
auto b = Fgs(10);
assert(a != b);
assert(a > b);
}
这是 DMD 的输出:
/home/mfag/lighthouse/testss.d(15): Error: template testss.Fgs.opEquals does not match any function template declaration
/home/mfag/lighthouse/testss.d(10): Error: template testss.Fgs.opEquals() cannot deduce template function from argument types !()(const(Fgs))
最佳答案
const
在编写时没有考虑在内。这应该有效:
bool opEquals()(auto const ref Fgs another) const
但这是语言的一个有点不稳定的领域,将来可能会在非模板上使用 auto ref
。
关于d - 无法覆盖结构的 opCmp 和 opEquals,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14042298/
我不能使用非基本类型作为关联数组的键;尝试这样做将导致我定义 AA 的行出现以下错误: Error: AA key type MyString does not have 'bool opEquals
来自 D 的 documentation : Alternatively you can declare a single templated opEquals function with an au
我正在尝试使用 SDL2 的 Derelict3 绑定(bind)编写一个简单的 hello world。我在链接期间遇到以前从未见过的错误。 module main; import derelict
我是一名优秀的程序员,十分优秀!