On Wed, 2014-11-26 at 11:20 -0600, Dirk Eddelbuettel wrote: > On 26 November 2014 at 10:15, Dirk Eddelbuettel wrote: > | > | Hi Martyn, > | > | On 26 November 2014 at 13:20, Martyn Plummer wrote: > | | This is related to David Shih's thread about modifying input arguments > | | but I think it needs its own thread. > | | > | | I found a problem when I tried porting some Rcpp code to run outside of > | | Rcpp by providing my own NumericVector implementation based on STL > | > | Interesting. I have seen that use case. Typically I have 'real' C++ code > > A "not" was missing there: "I have not seen that use case". Sorry, Dirk
I got that. It is an unusual use case, but not crazy. We have a complex likelihood function that we want to expose to R's optimization methods for maximum likelihood, but we also want to drop the same code into JAGS for Bayesian analysis via MCMC. The current code uses exclusively (Rcpp)Armadillo which works transparently in both environments so there is now no need for any emulation. > | using STL which I'd then bring to R(cpp). > | > | | vectors. In Rcpp it is possible to obtain a non-constant iterator for a > | | constant vector, whereas STL does not allow this. > | | > | | In particular, it is possible to modify a const NumericVector. The > | > | "Not on purpose" but mostly because we get a SEXP we cannot protect better? > | > | | compiler allows this because the actual contents of the NumericVector > | | are in auxiliary memory. But this breaks the abstraction of the > | | NumericVector class. > | | > | | An example is given below. The "sumsquares" function calculates the sum > | | of squares of a vector. Internally, it calls the "workhorse" function, > | | which is declared to take a constant reference as an argument. So it > | | looks safe to pass the input vector to "workhorse" without copying. > | | However the implementation of "workhorse" does in fact alter the vector. > | > | Isn't that an argument for just using STL vectors ? > | > | Otherwise, patches/pull requests always welcome... Sure. Martyn > | Dirk > | > | | > | | ### BEGIN SS.cc > | | #include <Rcpp.h> > | | > | | using Rcpp::NumericVector; > | | > | | static double workhorse(NumericVector const &v); > | | > | | // [[Rcpp::export]] > | | double sumsquares(NumericVector v) > | | { > | | //Since workhorse takes a constant reference we think we do not need > | | //to copy v... > | | return workhorse(v); > | | } > | | > | | double workhorse(NumericVector const &v) > | | { > | | double y = 0; > | | for (NumericVector::iterator i = v.begin(); i != v.end(); ++i) { > | | double &x = *i; > | | //... but this function does alter its argument > | | x *= x; > | | y += x; > | | } > | | return y; > | | } > | | ### END SS.cc > | | > | | In R we have > | | > | | > library(Rcpp) > | | > sourceCpp("SS.cc") > | | > x <- c(1,2,3) > | | > sumsquares(x) > | | [1] 14 > | | > x #is modified > | | [1] 1 4 9 > | | > | | Martyn > | | ----------------------------------------------------------------------- This message and its attachments are strictly confidential. If you are not the intended recipient of this message, please immediately notify the sender and delete it. Since its integrity cannot be guaranteed, its content cannot involve the sender's responsibility. Any misuse, any disclosure or publication of its content, either whole or partial, is prohibited, exception made of formally approved use ----------------------------------------------------------------------- _______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel