[Rcpp-devel] Forcing a shallow versus deep copy

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jul 11 14:38:09 CEST 2013


On Thu, Jul 11, 2013 at 8:32 AM, Changi Han <changi.han.wen at gmail.com> wrote:
> Hello,
>
> I think I (superficially) understand the difference between:
>
> // [[Rcpp::export]]
> double sum1(Rcpp::NumericMatrix M) {
>     arma::mat A(M.begin(), M.rows(), M.cols(), false);
>     return sum(sum(A));
> }
>
>
> // [[Rcpp::export]]
> double sum2(arma::mat A) {
>     return sum(sum(A));
> }
>
> Partly out of laziness, partly because sum2 is more elegant, and partly to
> avoid namespace pollution, I was wondering if there is a way to "force" a
> "shallow" copy in sum2.
>
> If not, then may I submit a low priority feature request. An attribute? Some
> thing like:
>
> // [[Rcpp::export]]
> double sum2(arma::mat A) {
>     // [[ Rcpp::shallow ( A ) ]]
>     return sum(sum(A));
> }
>
> Or (akin to C++11 generalized attributes)
>
> // [[Rcpp::export]] { [[ Rcpp::shallow ( A ) ]] }
> double sum2(arma::mat A) {
>     return sum(sum(A));
> }
>
> An alternative is to have an argument in sourceCpp that takes a list/vector
> of objects that are to be shallow or deep copied.
>
> For example in sum1, if M is changed within the function before casting to
> the arma::mat, then might be cleaner to add M to a list/vector of objects to
> be deep copied rather than cloning M within sum1: leads to one fewer
> variable name.
>
> Just a thought. I can certainly live with the additional step. As always,
> thanks for all the Rcpp goodness.

I had just made a similar suggestion in my post about half an hour ago.


More information about the Rcpp-devel mailing list