<div dir="ltr">Hi folks,<div><br></div><div>First, thanks for an amazing package! This is my first post to the Rcpp listserv, so please forgive me if I'm missing something obvious.</div><div><br></div><div>I've just noticed what, to me at least, is rather unexpected behaviour, as follows:</div><div><br></div><div># Here's the Rcpp function</div><div><div>library(Rcpp)</div><div><br></div><div><div>cppFunction('IntegerVector test_int_vec(IntegerVector x) {</div><div>  for ( R_xlen_t i = 0; i < x.size(); i++ )</div><div>    x[i] = x[i] * 2;</div><div>  return x;</div><div>}')</div></div><div><br></div><div># This works as expected</div><div><div>> a <- c(1, 2, 3, 4, 5)</div><div>> a</div><div>[1] 1 2 3 4 5</div><div>> test_int_vec(a)</div><div>[1]  2  4  6  8 10</div><div>> a</div><div>[1] 1 2 3 4 5</div><div><br></div><div># But this doesn't</div><div>> b <- c(1L, 2L, 3L, 4L, 5L)</div><div>> b</div><div>[1] 1 2 3 4 5</div><div>> test_int_vec(b)</div><div>[1]  2  4  6  8 10</div><div>> b</div><div>[1]  2  4  6  8 10</div></div><div><br></div><div><br></div><div>You'll see that in the first example, in which I pass a numeric vector, the function acts as I'd expect it, namely, it returns the result of the calculation, leaving the original vector unmodified. However, in the second example, when I pass it an integer vector, it modifies the vector in place, and the original vector is changed.</div><div><br></div><div>I can only imagine this has something to do with implicit coercion: when the reals are coerced to integers in the first example, the vector must be cloned, whereas in the second example, in which no coercion is necessary, it's a pointer to the original data. Another thing that leads me to believe this is that the same thing happens in reverse if you change the return/variable type of the function to NumericVector--reals end up being modified in place and integers aren't.</div><div><br></div><div>In my mind, one of the following things should happen:</div><div>1) In both situations, the vector is modified in place.</div><div>2) In both situations, the vector is copied--probably this isn't right, as it makes it impossible to modify in place.</div><div>3) Allow modification in place, but throw an error if the wrong type is passed.</div><div><br></div><div>Is this a bug? Am I just misunderstanding? Any help would be appreciated!</div><div><br></div><div>Thanks in advance and all best,</div><div><br></div><div>Evan</div><div><br></div><div>=====</div><div><br></div><div><div>> sessionInfo()</div><div>R version 3.3.2 (2016-10-31)</div><div>Platform: x86_64-w64-mingw32/x64 (64-bit)</div><div>Running under: Windows 7 x64 (build 7601) Service Pack 1</div><div><br></div><div>locale:</div><div>[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          </div><div>[5] LC_TIME=English_United States.1252    </div><div><br></div><div>attached base packages:</div><div>[1] stats     graphics  grDevices utils     datasets  methods   base     </div><div><br></div><div>other attached packages:</div><div>[1] Rcpp_0.12.10</div><div><br></div><div>loaded via a namespace (and not attached):</div><div>[1] tools_3.3.2     withr_1.0.2     memoise_1.0.0   digest_0.6.12   devtools_1.12.0</div></div><div><br></div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:small">Evan Cortens, PhD</span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:12.8px"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:x-small">Institutional Analyst - Office of Institutional Analysis</span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:12.8px"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:x-small">Mount Royal University</span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:12.8px"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:x-small">403-440-6529</span><br></div></div></div></div></div></div></div></div>
</div></div>