mardi 3 novembre 2015

Rcpp and move semantic

I implemented an algorithm in C++ that returns as output a huge array of elements. Now, I would like to implement a wrapper in Rcpp so that I will be able to call this function by using R.

I specified in the Makevars file the following setting:

PKG_CXXFLAGS = -std=c++11

So that I can use the C++11 version.

// [[Rcpp::export]]
NumericMatrix compute(int width, int height)
{
  vector<data_t> weights(width * height);
  compute_weights(weights);

  NumericMatrix mat(height, width);
  copy(begin(weights), end(weights), mat.begin());

  return mat;
}

The above wrapper function remains efficient if the NumericMatrix is moved when returned by the function, otherwise a new object will be created.

Does Rcpp exploit the move semantics? And if not, are there any workarounds to avoid the construction of the copy?

Aucun commentaire:

Enregistrer un commentaire