[Rcpp-devel] ivec and imat don't pass by reference

Serguei Sokol serguei.sokol at gmail.com
Tue Sep 15 12:16:34 CEST 2015


Hi,

If we declare the following toy function where a numeric matrix is passed by reference,
it works as expected, the matrix m is modified "in place":

cppFunction('
mat m2(mat& m) {
    m=m*2;
    return(m);
}', depends="RcppArmadillo", inc="using namespace arma;")
m=matrix(1:4, 2)+pi
m2(m)
           [,1]     [,2]
[1,]  8.283185 12.28319
[2,] 10.283185 14.28319
m
           [,1]     [,2]
[1,]  8.283185 12.28319
[2,] 10.283185 14.28319

But if we pass to imat type, the call by reference is no more functionning:
cppFunction('
imat im2(imat& m) {
    m=m*2;
    return(m);
}', depends="RcppArmadillo", inc="using namespace arma;")
im=matrix(1:4, 2)
im2(im)
      [,1] [,2]
[1,]    2    6
[2,]    4    8
im
      [,1] [,2]
[1,]    1    3
[2,]    2    4

The original matrix im remains as it was.
You can easyly verify that the same is true for ivec type.
Is it intended behaviour?

Serguei.
R version 3.2.0 (2015-04-16)
 > packageVersion("Rcpp")
[1] ‘0.12.0’
 > packageVersion("RcppArmadillo")
[1] ‘0.5.500.2.0’




More information about the Rcpp-devel mailing list