JuliaInterop / libcxxwrap-julia

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

std::copy #26

Closed kalmarek closed 4 years ago

kalmarek commented 5 years ago

a simple application of std::copy

std::vector<std::string>
copy_vector(jlcxx::ArrayRef<std::string> parameters)
{
    size_t                   len = parameters.size();
    std::vector<std::string> return_vector(len);
    std::copy(parameters.begin(), parameters.end(), return_vector.begin());
    // for (size_t i=0; i < len; i++){
    //     return_vector[i] = parameters[i];
    // }
    return return_vector;
}

produces an error:

required from here
/usr/include/c++/8.3.0/bits/stl_algobase.h:322:31: error: no match for ‘operator-’ (operand types are ‘jlcxx::array_iterator_base<_jl_value_t*, std::__cxx11::basic_string<char> >’ and ‘jlcxx::array_iterator_base<_jl_value_t*, std::__cxx11::basic_string<char> >’)
    for(_Distance __n = __last - __first; __n > 0; --__n)

this is because https://github.com/JuliaInterop/libcxxwrap-julia/blob/3818cbe8615c41d7f1d9b557cb85e66953494cde/include/jlcxx/array.hpp#L305

templates always as array_iterator_base<T,T>; e.g. changing the definition of operator- to

template<typename T, typename S>
std::ptrdiff_t operator-(const array_iterator_base<T,S>& l, const array_iterator_base<T,S>& r)
{
  return l.ptr() - r.ptr();
}

fixes the above.

It seems that using array_iterator_base<T,S> seems a reasonable choice, but I don't really feel competent in C++. @barche could You have a look at those template parameters?

kalmarek commented 5 years ago

@barche could you have a look at this? I got hit by it again:

jlcxx::ArrayRef<std::string> jlvec;
std::vector<std::string> vec(jlvec.begin(), jlvec.end());

results in

error: no match for ‘operator!=’ 
(operand types are ‘jlcxx::array_iterator_base<_jl_value_t*, std::__cxx11::basic_string<char> >’ and 
‘jlcxx::array_iterator_base<_jl_value_t*, std::__cxx11::basic_string<char> >’)
   82 |        for (; __first != __last; ++__first, (void)++__cur)

again, changing https://github.com/JuliaInterop/libcxxwrap-julia/blob/43eb10052af3f25ad7f4efec7817f115e7b7f6b2/include/jlcxx/array.hpp#L371 to

template<typename L, typename R>
bool operator!=(const array_iterator_base<L,R>& l, const array_iterator_base<L,R>& r)
{
  return r.ptr() != l.ptr();
}

fixes the issue

kalmarek commented 4 years ago

fixed by #33