Dear all, I am trying to write an Rcpp function that accepts optionally a vector, but I cannot figure out what default value I should give to it.
First I tried the following: // [[Rcpp::export]] int nilVec(NumericVector v = R_NilValue){ int sum = 0; for (int i = 0; i < v.length(); ++i){ sum += v[i]; } return sum; } but when trying: nilVec() I get the error "Not compatible with REALSXP", which is maybe not very consistent with the fact that in R you can do as.numeric(NULL) and it gives you a vector of length 0. Then I tried: // [[Rcpp::export]] int nilVec(NumericVector v = NumericVector()){ int sum = 0; for (int i = 0; i < v.length(); ++i){ sum += v[i]; } return sum; } but it doesn't compile. I found the following discussion: http://comments.gmane.org/gmane.comp.lang.r.rcpp/5922 where I discovered that for whatever reason the following compiles and works: // [[Rcpp::export]] int nilVec(NumericVector v = NumericVector::create(0)){ int sum = 0; for (int i = 0; i < v.length(); ++i){ sum += v[i]; } return sum; } But the funny thing is that this does not work: // [[Rcpp::export]] int nilMat(NumericMatrix v = NumericMatrix::create(0)){ int sum = 0; for (int i = 0; i < v.length(); ++i){ sum += v[i]; } return sum; } So how should I do for matrices? I would also be happy with the solution of defining somewhere an empty vector or an empty matrix and assigning it as default argument, but the following does not work: NumericVector empty(0); // [[Rcpp::export]] int nilVec(NumericVector v = empty){ int sum = 0; for (int i = 0; i < v.length(); ++i){ sum += v[i]; } return sum; } Suggestions? I would be super happy if in the future the special value R_NilValue could be converted to an empty vector, it would make things very easy and coherent with R's behaviour. Thx in advance, Ale -- Alessandro Mammana, PhD Student Max Planck Institute for Molecular Genetics Ihnestraße 63-73 D-14195 Berlin, Germany _______________________________________________ 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