# Puzzling behavior of icxx

**URL:** <https://discourse.julialang.org/t/puzzling-behavior-of-icxx/29950>\
**Category:** General Usage\
**Tags:** cxx\
**Created:** [October 15, 2019, 5:45pm UTC](https://discourse.julialang.org/t/puzzling-behavior-of-icxx/29950 "2019-10-15T17:45:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josiah\_Slack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josiah_slack/32/8319_2.png) [@Josiah\_Slack](https://discourse.julialang.org/u/Josiah_Slack)\
**Post date:** [October 15, 2019, 5:45pm UTC](https://discourse.julialang.org/t/puzzling-behavior-of-icxx/29950/1 "2019-10-15T17:45:13Z")

</div>

I’m using Cxx, and my use case involves converting std::vector return values into Julia arrays. I’m able to define my functions in the REPL, and everything behaves as I would expect. However, if I wrap all this in a module, I get a fatal error at an invocation of icxx"". I’ve been able to replicate this behavior with a reasonably simple test case:

> module Vector2Vector
> 
> using Cxx
> 
> export xformVectors, defineSummer
> 
> function defineSummer()  
> cxx"“”  
> #include   
> #include
> 
> class Summer {  
> public:  
> Summer() {}  
> std::vector compute\_sum(const std::vector\<std::vector\> &input) {  
> std::vector 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;  
> }  
> };  
> “”"  
> end
> 
> function xformVectors()
> 
> ```
> 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
> 
> end # module

If, in the REPL, I now type:

> using Vector2Vector:, defineSummer, xformVectors  
> defineSummer()  
> xformVectors()

I get this error:

> 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 …/.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 …/sandbox/julia/Vector2Vector.jl/src/Vector2Vector.jl:33  
> [6] top-level scope at none:0

Line 33 in Vector2Vector.jl is:

> cxx\_v = icxx"$summer-\>compute\_sum($x);"

The version of Julia I’m running is 1.1.1 and 0.3.2 of Cxx. I’ve tried running with Debugger.jl, but haven’t learned anything helpful.

---

<div class="post-metadata">

**Author:** ![Josiah\_Slack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josiah_slack/32/8319_2.png) [@Josiah\_Slack](https://discourse.julialang.org/u/Josiah_Slack)\
**Post date:** [October 15, 2019, 7:35pm UTC](https://discourse.julialang.org/t/puzzling-behavior-of-icxx/29950/2 "2019-10-15T19:35:32Z")

</div>

I’m still puzzled by the behavior of icxx, but in the example (and in my real code that I was condensing) it’s not actually needed. I can just use the @cxx macro directly:

> cxx\_v = @cxx summer-\>compute\_sum(x)

And all as well.
