In C++, we can declare a variable as a reference.
int a = 10;
int& b = a;
If we set b=15
, a
also changes.
I want to do the similar thing in Rcpp.
List X = obj_from_R["X"];
IntegerVector x_i = X[index];
x_i = value;
I want to update an object from R called X
by inserting a value to one of its vector. The code above did not work, so I tried this:
IntegerVector& x_i = X[index];
and received an error.
error: non-const lvalue reference to type 'IntegerVector'
(aka 'Vector<13>') cannot bind to a temporary of type 'Proxy' (aka 'generic_proxy<19>')
This question gets asked a lot in differing variants.
Here are some popular answers:
More details...
From the FAQ entry Rcpp changed the (const) object I passed by value that I wrote:
So, the
&
is just visual sugar as Rcpp objects already act as references.Thus, the following would show the conclusion:
Example:
The following Rcpp proxy model slides I created should further illustrate what is happening
Source: https://twitter.com/axiomsofxyz/status/938881541396197377