gpt4 book ai didi

d - 无法定义关联数组类型 : opEquals doesn't exist

转载 作者:行者123 更新时间:2023-12-04 02:41:22 26 4
gpt4 key购买 nike

我不能使用非基本类型作为关联数组的键;尝试这样做将导致我定义 AA 的行出现以下错误:

Error: AA key type MyString does not have 'bool opEquals(ref const MyString) const

我第一次发现这个是在使用 CollisionHandler[Tuple!(TypeInfo, TypeInfo)] 类型时,其中 CollisionHandler 是函数指针类型的别名。

但是,即使是 Associative array documentation page 下“使用结构或联合作为 KeyType”标题中的示例代码失败并出现相同的错误:

import std.string;

struct MyString
{
string str;

size_t toHash() const @safe pure nothrow
{
size_t hash;
foreach (char c; str)
hash = (hash * 9) + c;
return hash;
}
// opEquals defined here?
bool opEquals(ref const MyString s) const @safe pure nothrow
{
return std.string.cmp(this.str, s.str) == 0;
}
}

int[MyString] foo; // errors here

void main() {

}

这里,MyString.opEquals 被定义并且应该有正确的签名,但是 dmd 编译器说它没有实现。这个片段直接来自文档这一事实让我怀疑这是一个编译器错误,但也许我只是遗漏了什么?

在Linux下运行DMD,在Windows 7下也会出现这个问题。DMD版本:

$ dmd --help
DMD64 D Compiler v2.066.1
Copyright (c) 1999-2014 by Digital Mars written by Walter Bright
Documentation: http://dlang.org/
...

最佳答案

这是编译器发出误导性错误消息的情况。

问题出在 opEquals 方法上的 @safe 注释。在 2.066.1 中,std.string.cmp 不是 @safe - 但是,编译器会显示错误的错误消息。如果您将 opEquals 重命名为其他名称,例如foo,你会得到不同的错误信息:

test.d(19): Error: safe function 'test.MyString.foo' cannot call system function
'std.algorithm.cmp!("a < b", string, string).cmp'

解决方法是删除 @safe,或将其替换为 @trusted

注意这个问题在DMD的开发版中没有出现,所以应该在2.067.0中修复。

关于d - 无法定义关联数组类型 : opEquals doesn't exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27975267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com