Julia Cxx 在模块中打包不同的行为

julia Cxx package different behavior when in module

本文关键字:包不同 Cxx 模块 Julia      更新时间:2023-10-16

我正在使用Julia 1.1.1和Cxx的0.3.2版本。我发现当我从我定义的模块中调用 icxx" 时出现错误,但在 REPL 中调用它时则没有。在 REPL 中,我有:

using Cxx
function xformVectors()
cxx"""
#include <iostream>
#include <vector>
class Summer {
public:
Summer() {}
std::vector<int> compute_sum(const std::vector<std::vector<int>> &input) {
std::vector<int> result(input.size(), 0);
for (std::size_t i = 0; i != input.size(); ++i) {
for (std::size_t j = 0; j != input[i].size(); ++j) {
result[i] += input[i][j];
}
}
return result;
}
};
"""
as = [rand(1:10,5), rand(1:10,6)]
x = convert(cxxt"std::vector< std::vector< int > >", as)
summer = @cxxnew Summer()
cxx_v = icxx"$summer->compute_sum($x);"
for v in cxx_v
println(collect(v))
end
end

如果我然后调用xformVectors(),它会写出几个整数并退出(预期行为(。如果我使用 Pkg.generate(( 将此代码包装在模块中,然后调用xformVectors(),则会出现致命错误:

ERROR: BoundsError: attempt to access 36-element Array{Tuple{AbstractString,Symbol,Int64,Int64,Bool},1} at index [37]
Stacktrace:
[1] getindex(::Array{Tuple{AbstractString,Symbol,Int64,Int64,Bool},1}, ::Int64) at ./array.jl:729
[2] #s37#70 at /home/jov9025/.julia/packages/Cxx/vxYtJ/src/cxxstr.jl:705 [inlined]
[3] #s37#70(::Any, ::Any, ::Any, ::Any) at ./none:0
[4] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at ./boot.jl:522
[5] xformVectors() at /home/jov9025/sandbox/julia/Vector2Vector.jl/src/Vector2Vector.jl:30
[6] top-level scope at none:0

如果在 REPL 中包含源文件 (include("src/Vector2Vector.jl"(,然后调用Vector2Vector.xformVectors(),它运行而不会抱怨。

知道发生了什么吗?

我的代码中产生错误的行是:

cxx_v = icxx"$summer->compute_sum($x);" 

我仍然不明白 icxx"的问题是什么,但我实际上不需要在此示例(或我的真实代码(中使用它。我只能使用@cxx宏,瞧,没有崩溃:

cxx_v = @cxx compute_sum(x)

所以,我可能问错了问题(尽管我认为这是一个有趣的问题(。

在 2020 年 1 月重新审视这一点:使用 Cxx 的 0.3.3 版,代码在 REPL 中的原始 icxx" 行中执行得很好。