cpp11 icon indicating copy to clipboard operation
cpp11 copied to clipboard

std::max_element fails on writable vectors

Open thomasp85 opened this issue 2 years ago • 0 comments
trafficstars

Using the std::max_element() algorithm on a writable vector results in a compile error. The error is not present when using a non-writable vector. It is speculated that the following commit made it possible on the standard vectors and need to be applied to the writable versions as well: https://github.com/r-lib/cpp11/commit/686ae04f4c239735bdbe3e462731ae650ec6a225

#include <cpp11/doubles.hpp>
[[cpp11::register]]
void test() {
  cpp11::writable::doubles foo = {1, 2, 3, 4, 5};
  std::max_element(foo.begin(), foo.end());
}

Compiling the above yields the following error:

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/max_element.h:34:25: error: object of type 'cpp11::writable::r_vector<double>::iterator' cannot be assigned because its copy assignment operator is implicitly deleted
                __first = __i;
                        ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/max_element.h:44:19: note: in instantiation of function template specialization 'std::__max_element<std::__less<cpp11::writable::r_vector<double>::proxy> &, cpp11::writable::r_vector<double>::iterator>' requested here
    return _VSTD::__max_element<_Comp_ref>(__first, __last, __comp);
                  ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/max_element.h:52:19: note: in instantiation of function template specialization 'std::max_element<cpp11::writable::r_vector<double>::iterator, std::__less<cpp11::writable::r_vector<double>::proxy>>' requested here
    return _VSTD::max_element(__first, __last,
                  ^
code.cpp:4:8: note: in instantiation of function template specialization 'std::max_element<cpp11::writable::r_vector<double>::iterator>' requested here
  std::max_element(foo.begin(), foo.end());
       ^
/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/cpp11/include/cpp11/r_vector.hpp:259:21: note: copy assignment operator of 'iterator' is implicitly deleted because field 'data_' is of reference type 'const cpp11::writable::r_vector<double> &'
    const r_vector& data_;
                    ^
1 error generated.

Whereas this compiles fine

void test(cpp11::doubles foo) {
  std::max_element(foo.begin(), foo.end());
}

thomasp85 avatar Aug 28 '23 06:08 thomasp85