libcxxwrap-julia icon indicating copy to clipboard operation
libcxxwrap-julia copied to clipboard

Have `jlcxx::Array` be a bit more like a Container

Open rgcv opened this issue 5 years ago • 0 comments

Specifically, as described in cppreference. A minor change such as typedefing the container's value_type already allows the usage of std::back_inserter.

As an example of such usage, here's a use case of mine - calling a function requiring an OutputIterator as argument:

mod.method("oifunc", [](jlcxx::ArrayRef<int> is) {
  jlcxx::Array<int> res;
  oifunc(is.begin(), is.end(), std::back_inserter(res));
  return res;
});

Otherwise, I'd probably need to do something like the following (if I still want to return a julia array, i.e. jlcxx::Array):

mod.method("oifunc", [](jlcxx::ArrayRef<int> is) {
  std::vector<int> res;
  jlcxx::Array<int> jlres;

  oifunc(is.begin(), is.end(), std::back_inserter(res));

  auto it = res.begin();
  while (it != res.end()) jlres.push_back(*it++);
  return jlres;
});

EDIT: Assuming the following definition:

template<typename InputIterator, typename OutputIterator>
oifunc(InputIterator begin, InputIterator end, OutputIterator out);

I'm not sure if making types like jlcxx::Array closer to STL-like spec is in the horizon, seeing one could just leverage the already in-place STL capabilities and just using jlcxx::apply_stl<T>(). Nonetheless, it would make usage more practical.

Addendum: Albeit slightly unrelated, I think this issue pairs nicely with #58.

rgcv avatar Jul 01 '20 16:07 rgcv