# How to wrap a C++ class with a templated constructor

**URL:** <https://discourse.julialang.org/t/how-to-wrap-a-c-class-with-a-templated-constructor/15364>\
**Category:** General Usage\
**Created:** [September 23, 2018, 2:59am UTC](https://discourse.julialang.org/t/how-to-wrap-a-c-class-with-a-templated-constructor/15364 "2018-09-23T02:59:28Z")\
**Posts on this page:** 6\
**Page:** 1

<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:** [September 23, 2018, 2:59am UTC](https://discourse.julialang.org/t/how-to-wrap-a-c-class-with-a-templated-constructor/15364/1 "2018-09-23T02:59:28Z")

</div>

I’m trying to wrap the fastjet library in C++, using CxxWrap.  
I’ve been trying to figure out how to wrap a templated constructor for [this class](http://www.fastjet.fr/repo/doxygen-3.3.1/classfastjet_1_1ClusterSequence.html), but I’m failing.  
[My attempt](https://github.com/jstrube/FastJetWrapBuilder/blob/master/src/fastJetWrap.cc#L102) to just let the compiler figure this out is not met favorably. Is this currently possible? @barche

```julia
julia: /afs/desy.de/user/j/jstrube/.julia/packages/CxxWrap/Ho7vF/deps/usr/include/jlcxx/type_conversion.hpp:325: static jl_datatype_t* jlcxx::static_type_mapping<SourceT, Enable>::julia_type() [with SourceT = std::vector<fjcore::PseudoJet>; Enable = void; jl_datatype_t = _jl_datatype_t]: Assertion `type_pointer() != nullptr' failed.

signal (6): Aborted
in expression starting at /afs/desy.de/user/j/jstrube/.julia/dev/FastJet/src/FastJet.jl:9
gsignal at /lib64/libc.so.6 (unknown line)
abort at /lib64/libc.so.6 (unknown line)
__assert_fail_base at /lib64/libc.so.6 (unknown line)
__assert_fail at /lib64/libc.so.6 (unknown line)
_ZN5jlcxx19static_type_mappingISt6vectorIN6fjcore9PseudoJetESaIS3_EEvE10julia_typeEv at /afs/desy.de/user/j/jstrube/.julia/dev/FastJet/deps/usr/lib/libfastjetwrap.so (unknown line)
_ZN5jlcxx10julia_typeISt6vectorIN6fjcore9PseudoJetESaIS3_EEEEP14_jl_datatype_tv at /afs/desy.de/user/j/jstrube/.julia/dev/FastJet/deps/usr/lib/libfastjetwrap.so (unknown line)
_ZN5jlcxx6detail14argtype_vectorIJRKSt6vectorIN6fjcore9PseudoJetESaIS4_EERKNS3_13JetDefinitionEEEES2_IP14_jl_datatype_tSaISD_EEv at /afs/desy.de/user/j/jstrube/.julia/dev/FastJet/deps/usr/lib/libfastjetwrap.so (unknown line)
_ZNK5jlcxx15FunctionWrapperIP11_jl_value_tJRKSt6vectorIN6fjcore9PseudoJetESaIS5_EERKNS4_13JetDefinitionEEE14argument_typesEv at /afs/desy.de/user/j/jstrube/.julia/dev/FastJet/deps/usr/lib/libfastjetwrap.so (unknown line)
register_julia_module at /afs/desy.de/user/j/jstrube/.julia/packages/CxxWrap/jLyxA/deps/usr/lib/libcxxwrap_julia.so (unknown line)
register_julia_module at /afs/desy.de/user/j/jstrube/.julia/packages/CxxWrap/jLyxA/src/CxxWrap.jl:151 [inlined]
readmodule at /afs/desy.de/user/j/jstrube/.julia/packages/CxxWrap/jLyxA/src/CxxWrap.jl:378
wrapmodule at /afs/desy.de/user/j/jstrube/.julia/packages/CxxWrap/jLyxA/src/CxxWrap.jl:384
jl_fptr_trampoline at /buildworker/worker/package_linux64/build/src/gf.c:1829
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2182
do_call at /buildworker/worker/package_linux64/build/src/interpreter.c:324
eval_value at /buildworker/worker/package_linux64/build/src/interpreter.c:428
eval_stmt_value at /buildworker/worker/package_linux64/build/src/interpreter.c:363 [inlined]
eval_body at /buildworker/worker/package_linux64/build/src/interpreter.c:686
and so on...

```

---

<div class="post-metadata">

**Author:** ![groot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/groot/32/13081_2.png) [@groot](https://discourse.julialang.org/u/groot)\
**Post date:** [September 24, 2018, 6:20pm UTC](https://discourse.julialang.org/t/how-to-wrap-a-c-class-with-a-templated-constructor/15364/2 "2018-09-24T18:20:25Z")

</div>

A workaround is here [libcxxwrap-julia/types.cpp at main · JuliaInterop/libcxxwrap-julia · GitHub](https://github.com/JuliaInterop/libcxxwrap-julia/blob/master/examples/types.cpp#L257) .

---

<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:** [September 24, 2018, 6:39pm UTC](https://discourse.julialang.org/t/how-to-wrap-a-c-class-with-a-templated-constructor/15364/3 "2018-09-24T18:39:56Z")

</div>

I don’t understand how this solves my problem.  
I know how to implement a method. I don’t know how to implement the templated constructor.

I guess I could write a factory method, give it some other name and just return the type…

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [September 24, 2018, 7:50pm UTC](https://discourse.julialang.org/t/how-to-wrap-a-c-class-with-a-templated-constructor/15364/4 "2018-09-24T19:50:18Z")

</div>

The problem is the `std::vector<PsudoJet>`. You would need to either map it, since CxxWrap doesn’t (yet?) provide support for standard containers, or work around it by creating a constructor that takes an `ArrayRef` like the function @groot suggested. Note that in the context of [issue 112](https://github.com/JuliaInterop/CxxWrap.jl/issues/112) I am looking at making `ArrayRef` more user friendly. Also, when creating a factory method you can just name it the same as the type and it will look like an outer constructor to Julia.

---

<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:** [September 24, 2018, 9:02pm UTC](https://discourse.julialang.org/t/how-to-wrap-a-c-class-with-a-templated-constructor/15364/5 "2018-09-24T21:02:33Z")

</div>

Ah. Thanks for clarifying. Now that makes sense.  
And thanks for the suggestion regarding the name of the factory method. I was hoping something like that would work, so I was going to try that anyway, but great that you’re confirming that.

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [September 25, 2018, 6:06am UTC](https://discourse.julialang.org/t/how-to-wrap-a-c-class-with-a-templated-constructor/15364/6 "2018-09-25T06:06:29Z")

</div>

Forgot to add in my previous message: you can decipher the types in the error like this:

```julia
c++filt -t _ZN5jlcxx19static_type_mappingISt6vectorIN6fjcore9PseudoJetESaIS3_EEvE10julia_typeEv

```

which yields:

```nohighlight
jlcxx::static_type_mapping<std::vector<fjcore::PseudoJet, std::allocator<fjcore::PseudoJet> >, void>::julia_type()

```
