<div dir="ltr"><div>Hello,</div><div><br></div><div>With the following inplace sorting example, I understand the value of `a` is sorted inplace, but it's strange to see the value of `b` is also modified. This can cause some hard to detect bug, since the cpp function may modify a variable defined in other scope.</div><div><br></div><div>It seems that rcpp doesn't respect the named field, which is adopted by R to implement copy-on-modify. I don's see an easy fix on C++ side, since the called cpp function has no information about variable binding in R. A possible fix is adding a function `inplace` to R, which ensure the returned variable has named filed = 0 so is safe to modify inplace. Then, we have to call the function as `stl_sort_inplace(inplace(a))`, which seems odd but is also informative. It shows clearly that we are breaking the pass-by-value rule in R.</div><div><br></div><div>```cpp</div><div><div>#include <Rcpp.h></div><div>using namespace Rcpp;</div><div><br></div><div>// [[Rcpp::export]]</div><div>void stl_sort_inplace(NumericVector x) {</div><div>    std::sort(x.begin(), x.end());</div><div>}</div></div><div><br></div><div>```</div><div><br></div><div>```r</div><div><div>a <- seq(1, 0.1, -0.1)</div><div>b <- a</div><div>#  [1] 1.0 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1<br></div><div><br></div><div>stl_sort_inplace(a)</div><div><br></div><div>a</div><div>#  [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0<br></div><div><br></div><div>b</div></div><div>#  [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0</div><div><br></div><div><div>a <- seq(1, 0.1, -0.1)</div><div>pure_function <- function (x) {</div><div>  y <- x</div><div>  stl_sort_inplace(y)</div><div>  print(y)</div><div>}</div><div>pure_function(a)</div><div>a</div></div><div>#  [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0</div><div><br></div><div>```</div><div><br></div></div>