# Passing an array of a C++ class to julia (CxxWrap)

**URL:** <https://discourse.julialang.org/t/passing-an-array-of-a-c-class-to-julia-cxxwrap/17622>\
**Category:** General Usage\
**Tags:** question, cxx\
**Created:** [November 16, 2018, 10:12pm UTC](https://discourse.julialang.org/t/passing-an-array-of-a-c-class-to-julia-cxxwrap/17622 "2018-11-16T22:12:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)\
**Post date:** [November 16, 2018, 10:12pm UTC](https://discourse.julialang.org/t/passing-an-array-of-a-c-class-to-julia-cxxwrap/17622/1 "2018-11-16T22:12:56Z")

</div>

Hi,

I’ve been wrapping a C++ library with C++ wrap, and some of the methods in it return vectors, either of simple items like floats, and ints, or sometimes vectors of classes.

I’ve come up with a way to give a std::vector to julia by doing the following:

```nohighlight
.method("get_fw_nodes", [](SequenceGraph& sg, sgNodeID_t n) {
                std::vector<sgNodeID_t> fw_nodes = sg.get_fw_nodes(n);
                auto *nodes = new sgNodeID_t[fw_nodes.size()];
                std::copy(fw_nodes.begin(), fw_nodes.end(), nodes);
                auto jlarr = jlcxx::make_julia_array(nodes, fw_nodes.size());
                return jlarr;
            })

```

So I call the method, get the vector, allocate a new array, copy vector contents to the array, wrap that up in an ArrayRef and pass that to julia. This works great for this case as `sgNodeID_t` is just `int64_t`.  
The use of a new without a paired delete scares me, but I’m trusting julia gc to take care of that, as I believe `jccxx::make_julia_array` ensures the resulting Array is owned by julia and so should be gc’d.

I’d like to do something similar, passing an array of a C++ class. The class is wrapped in CxxWrap too:

```nohighlight
mod.add_type<Link>("Link")
            .constructor()
            .constructor<sgNodeID_t, sgNodeID_t, int32_t>();

```

However trying to do something like this:

```nohighlight
.method("get_fw_links", [](SequenceGraph& sg, sgNodeID_t n){
                std::vector<Link> fw_links = sg.get_fw_links(n);
                auto *links = new Link[fw_links.size()];
                std::copy(fw_links.begin(), fw_links.end(), links);
                auto jlarr = jlcxx::make_julia_array(links, fw_links.size());
                return jlarr;
            })

```

Doesn’t work. Is it at all possible using CxxWrap and any other julia c/c++ interfacing to pass to julia an array of C++ objects (which are themselves CxxWrapped)? I know an alternative route is to wrap std::vector.

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [November 16, 2018, 10:20pm UTC](https://discourse.julialang.org/t/passing-an-array-of-a-c-class-to-julia-cxxwrap/17622/2 "2018-11-16T22:20:53Z")

</div>

Or just allocate the vector on the julia side and pass it to `get_fw_links` to be initialized (since You already the size?)

---

<div class="post-metadata">

**Author:** ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)\
**Post date:** [November 16, 2018, 10:35pm UTC](https://discourse.julialang.org/t/passing-an-array-of-a-c-class-to-julia-cxxwrap/17622/3 "2018-11-16T22:35:01Z")

</div>

std::vector stores classes contiguously, whereas julia array’s don’t nessecerily do that. Presumably even if I allocated on the Julia side, say `Vector{LinkRef}` or `Vector{LinkAllocated}`, I would need some way on the C++ side, to box up each Link object so as it can be given to those julia arrays and not eliminated by C++ destructors?

---

<div class="post-metadata">

**Author:** ![jstrube](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jstrube/32/525_2.png) [@jstrube](https://discourse.julialang.org/u/jstrube)\
**Post date:** [November 16, 2018, 11:14pm UTC](https://discourse.julialang.org/t/passing-an-array-of-a-c-class-to-julia-cxxwrap/17622/4 "2018-11-16T23:14:51Z")

</div>

I’m not sure I understand why you want to copy the data in the first place.  
Please take a look here: [LCIO\_Julia\_Wrapper/MCParticle.icc at main · jstrube/LCIO\_Julia\_Wrapper · GitHub](https://github.com/jstrube/LCIOWrapBuilder/blob/master/src/MCParticle.icc#L20)  
This wraps a method that returns a vector of objects. `MCParticleVec` is just a typedef to an `std::vector<MCParticle>`. The C++ API is here: [http://lcio.desy.de/v02-09/doc/doxygen\_api/html/classEVENT\_1\_1MCParticle.html](http://lcio.desy.de/v02-09/doc/doxygen_api/html/classEVENT_1_1MCParticle.html)  
Then I just wrap `at` and `size` of the `std::vector` and make an iterator on the Julia side.  
Does that help?

---

<div class="post-metadata">

**Author:** ![stemann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stemann/32/4030_2.png) [@stemann](https://discourse.julialang.org/u/stemann)\
**Post date:** [December 4, 2018, 2:03pm UTC](https://discourse.julialang.org/t/passing-an-array-of-a-c-class-to-julia-cxxwrap/17622/5 "2018-12-04T14:03:48Z")

</div>

This approach (wrapping `at` and `length` as `getindex` and `length` and writing an iterator in Julia) worked out for me as well, but I would prefer a solution which could handle all `std::vector`’s in one go - preferably built-in to `CxxWrap`, similar to functionality in `Cxx`.

But in any case - I am very grateful for `CxxWrap` and the solution posted here.
