cpp11
cpp11 copied to clipboard
Importing more headers than required? [How to write a vector or integer's coordinates in R's cpp11?]
trafficstars
As the answer to my StackOverflow question says.
This example shows that the documentation could be improved.
[[cpp11::register]] integers dummyexample(integers x1, integers x2) {
int n = x1.size();
writable::integers y1(n);
writable::integers y2(n);
// add content to y2
// y2_1 = x2_1^2, y2_2 = x2_2^2, ...
for (int i = 0; i < n; i++) { y2[i] = pow(x2[i], 2); }
// then
for (int i = 0; i < n; i++) {
// error: ambiguous overload for ‘operator=’
// y1[i] = y2[i];
// this should resolve the ambiguity
// y1.at(i) = y2.at(i);
// not really, also error: ambiguous overload for ‘operator=’
// maybe
// y1.operator[](i).operator=(y2.operator[](i));
// not either
// int temp = y2[i];
// y1[i] = temp;
// 0 + y2_i works, but why?
y1[i] = 0 + y2[i];
}
return y1;
}
Taking from SamR's answer:
y1[i] and y2[i] are elements of writable integer vectors, and are of type cpp11::writable::r_vector
- r_vector.hpp line 241:
proxy& operator=(const T& rhs); - integers.hpp line 59:
inline typename r_vector<int>::proxy& r_vector<int>::proxy::operator=(const int& rhs)