Skip to content
Prev 49972 / 63424 Next

R-devel does not update the C++ returned variables

On 03/02/2015 11:39 AM, Dirk Eddelbuettel wrote:
avoiding duplication, harmless in the doubleThis() case, comes at some 
considerable hazard in general

 > Rcpp::cppFunction("IntegerVector incrThisAndThat(IntegerVector x) { x[0] += 
1; return x; }")
 > x = y = 1:5
 > incrThisAndThat(x)
[1] 2 2 3 4 5
 > x
[1] 2 2 3 4 5
 > y
[1] 2 2 3 4 5

(how often this happens in the now relatively large number of user-contributed 
packages using Rcpp?). It seems like 'one-liners' should really encourage 
something safer (sometimes at the expense of 'speed'),

   Rcpp::cppFunction("IntegerVector doubleThis(const IntegerVector x) { return x 
* 2; }")

   Rcpp::cppFunction("std::vector<int> incrThis(std::vector<int> x) { x[0] += 1; 
return x; }")

or that Rcpp should become more careful (i.e., should not allow!) modifying 
arguments with NAMED != 0.

Martin (Morgan)