JuliaInterop / libcxxwrap-julia

C++ library for backing CxxWrap.jl
Other
79 stars 43 forks source link

Missing `operator++(int)` for postfix `++` in `array_iterator_base` #58

Closed rgcv closed 4 years ago

rgcv commented 4 years ago

Assuming this is one of the insufficiencies of jlcxx::ArrayRef since it isn't strictly compatible with a typical STL container, or at least its iteration interface isn't.

I found an issue using array_iterator_base when presented with syntax such like *begin++, faced with the error no 'operator++'(int) declared for postfix '++'.

Currently, to circumvent this issue, I have to hack around it using a std::vector. Here's a hypothetical example of a method creation in module mod:


mod.method("work", [](jlcxx::ArrayRef<T> ts) {
  std::vector<T> v(ts.begin(), ts.end());
  target_function(v.begin(), v.end());
});

It's an easy hack, but seems unnecessary to introduce the additional complexity of rebuilding an entire STL container for the purposes of passing it immediately through to another function.

rgcv commented 4 years ago

I tried applying the following patch and it seems to be working.

diff --git a/include/jlcxx/array.hpp b/include/jlcxx/array.hpp
index 29b59ed..d3d9922 100644
--- a/include/jlcxx/array.hpp
+++ b/include/jlcxx/array.hpp
@@ -54,12 +54,26 @@ public:
     return *this;
   }

+  array_iterator_base<PointedT, CppT> operator++(int)
+  {
+    auto result(*this);
+    ++(*this);
+    return result;
+  }
+
   array_iterator_base<PointedT, CppT>& operator--()
   {
     --m_ptr;
     return *this;
   }

+  array_iterator_base<PointedT, CppT> operator--(int)
+  {
+    auto result(*this);
+    --(*this);
+    return result;
+  }
+
   array_iterator_base<PointedT, CppT>& operator+=(std::ptrdiff_t n)
   {
     m_ptr += n;

Feel free to use this or modify it at will :) I can also submit a PR if you prefer.