[Rcpp-devel] Call by reference

Dirk Eddelbuettel edd at debian.org
Tue Jul 28 20:45:55 CEST 2015


On 28 July 2015 at 17:53, Rguy wrote:
| I attempted to implement the call by reference example on page 200 of Seamless
| R and C++, as follows:
| 
| #include <Rcpp.h>
| using namespace Rcpp;
| 
| // [[Rcpp::export]]
| void absC(double & x) {
| if (x < 0) x = -x;
| }
| 
| /*** R
| x = -1
| absC(x)
| x
| */
| 
| Unfortunately, x remains equal to -1.
| Platforms: Windows 7,  "R version 3.1.3 Patched (2015-03-16 r68103)".
| Please advise.

An (atomic) double does not exist in R, so you are _always_ forcing a copy,
which works against your intent here.

Try replacing double with Rcpp::NumericVector.

Dirk


-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list