Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing more headers than required? [How to write a vector or integer's coordinates in R's cpp11?] #339

Closed
pachadotdev opened this issue Nov 7, 2023 · 0 comments · Fixed by #391

Comments

@pachadotdev
Copy link
Contributor

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::proxy. It appears that the equals operator for this type has been defined in two places:

  1. r_vector.hpp line 241: proxy& operator=(const T& rhs);
  2. integers.hpp line 59: inline typename r_vector<int>::proxy& r_vector<int>::proxy::operator=(const int& rhs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant