FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Importing more headers than required? [How to write a vector or integer's coordinates in R's cpp11?] · Issue #339 · r-lib/cpp11 · GitHub

/ cpp11 Public

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

Description

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL