CxxInterface.jl icon indicating copy to clipboard operation
CxxInterface.jl copied to clipboard

Simple example

Open gitboy16 opened this issue 3 years ago • 2 comments

Hi, thank you for the package. Someone mentionned your package: https://discourse.julialang.org/t/ccall-c-sort-vector-of-string/74635/21

I was wondering if you would be able to provide a simple example (step by step) on how to sort a vector of strings using c++ std::sort. Thank you

gitboy16 avatar Jan 17 '22 15:01 gitboy16

As described on Discourse, this question is somewhat ill-posed, i.e. there are details missing. Calling C++ requires handling many low-level details, and you still need to handle them when you call C++ from Julia. Such questions are:

  • Would you represent the vector and the string using Julia types or C++ types? Or can they be copied and converted for the call?
  • If these are C++ types, who owns them (who is responsible for deallocating them)? Are you passing pointers, references, copies, or would you use std::shared_ptr or std::unique_ptr?
  • Do you want to compare the string using Julia's string comparison or the C++ string comparison method?

If you are thinking of an existing C++ library, then looking at the library's API might help answer these questions.

If you are unfamiliar with the questions above, then some higher-level questions might help determine the answers:

  • Who creates (allocates) the vectors and strings? Julia, or the library?
  • How much time do these vectors and strings spend in C++? Are they only briefly passed for processing, or is the C++ library keeping them around for much longer?
  • How generic is the C++ library? Can it handle arbitrary vector or string types, or does it basically have to be std::vector and std::string? (Or are these actually C types, as in const char *vec[10]?

eschnett avatar Jan 17 '22 17:01 eschnett

I'll try to answer your questions but if you need more information please let me know. -Julia creates/allocates the vectors -The vectors are passed to C++ for processing and then sent back to Julia for more processing. Once sent back to Julia, C++ does not need to keep these vectors. -The library handles std::vector<std::string> types only.

Thank you

gitboy16 avatar Jan 17 '22 17:01 gitboy16