gpt4 book ai didi

templates - 使用数组将MapResult转换为数组

转载 作者:行者123 更新时间:2023-12-02 04:33:06 24 4
gpt4 key购买 nike

我在使用 std.array 中的 arrayMapResult 转换为特定数组类型时遇到了一些麻烦。我的问题如下:

我有一个对象数组a,每个对象都有一个可公开访问的字段val。我想使用 std.algorithm 中的 map 来遍历 a 并返回 val< 的所有值的数组 成员。我的代码看起来像这样:

import std.algorithm:map;
import std.array:array;
//import for my object type, which I call Box here

ulong[] fun (Box[] a) {
return array!(ulong[])(map!(function ulong(Box x) {return x.val;})(a);
}

但是,当我尝试执行此操作时,编译器会给出一个错误,指出 array 无法从参数类型 !(ulong[])(MapResult!(_funcliteral3 ,Box[]))。这是否意味着 MapResult 不是范围,有没有办法获得我想要的结果?

最佳答案

实际上这意味着编译器认为 (ulong[])(MapResult!(_funcliteral3,Box[])) 是模板参数,而不是 ulong[]

正确嵌套括号,它应该被修复

return array!(ulong[])(map!(function ulong(Box x) {return x.val;})(a));

关于templates - 使用数组将MapResult转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22599045/

24 4 0