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

failure on complex array

Open nbecker opened this issue 7 years ago • 6 comments

Code is here: https://gist.github.com/nbecker/e1d67619be96f788c6f8eba3cf9ca0d0

g++ and clang++ both have a similar complaint: /usr/include/boost/accumulators/statistics/sum.hpp:46:23: error: no match for 'operator+=' (operand types are 'std::complex' and 'const jlcxx::detail::JuliaComplex') this->sum += args[parameter::keyword<Tag>::get()];

I'm baffled - a very similar test "summer.cc" compiles fine. I don't know why the conversion here fails.

nbecker avatar Jun 20 '17 15:06 nbecker

Where exactly in your code does this happen? It definitely looks like a CxxWrap bug, the JuliaComplex type should never "escape" into your code but be converted to std::complex automatically, possibly I missed some case where a reference is taken.

barche avatar Jun 21 '17 22:06 barche

  mod.method ("call", [](accum_t& a, xt::jlarray<el_t> s) {
      for (auto e : s) a(e);  <<<< error is here
      return a;
    });

nbecker avatar Jun 21 '17 22:06 nbecker

I've updated the gist adding the complete compiler messages

nbecker avatar Jun 21 '17 22:06 nbecker

I have no idea why, but iterating over the jlarray seems to return the Julia type, so replacing your loop with the following seems to work:

for (auto e : s) a(jlcxx::convert_to_cpp<el_t>(e));

barche avatar Jun 22 '17 23:06 barche

But this doesn't fix this test:

  mod.method ("inc", [](xt::jltensor<el_t,1> s) {
      s[0] += 1;
    });

In this case, convert_to_cpp can't be used because we need an lvalue

nbecker avatar Jun 23 '17 11:06 nbecker

You can't modify the Julia value in-place because the numbers (both Float64 and Complex) are immutable. I think something like this should work:

s[0] = jlcxx::convert_to_julia(jlcxx::convert_to_cpp<el_t>(s[0]) + 1);

barche avatar Jun 24 '17 04:06 barche

@barche close?

fingolfin avatar Jan 05 '23 02:01 fingolfin