Rcpp: Question about NumericMatrix & IntergerMatrix

Created on 18 Jul 2018  ·  2Comments  ·  Source: RcppCore/Rcpp

Rcpp::cppFunction('
  void test_pointer(NumericMatrix &m){
      m[0] = 100;
  }
')

a1 = matrix(1:9, 3)
test_pointer(a1) 
a1  # no error and a1 does not change
a2 = a1 + 0.0
test_pointer(a2)  
a2 # a2 changes

I am new to c++ and Rcpp. It seems the test_pointer function does not work on IntergerMatrix input, why it runs successfully without any warning or error?

Most helpful comment

R creates a temporary NumericMatrix (converting from a1), which is passed to test_pointer, and then lost.

Note that you don’t need to write &m in your function definition, SEXP are references and Rcpp functions can modify their arguments.

All 2 comments

R creates a temporary NumericMatrix (converting from a1), which is passed to test_pointer, and then lost.

Note that you don’t need to write &m in your function definition, SEXP are references and Rcpp functions can modify their arguments.

Someone reminded me the other day that we apparently have an entry in the FAQ for this too.

Was this page helpful?
0 / 5 - 0 ratings