[Rcpp-devel] Const Reference Arguments for class methods in Rcpp-Module

schattenpflanze at arcor.de schattenpflanze at arcor.de
Sat Apr 23 15:43:10 CEST 2011


Hello,

here is yet another question. My C++ class A has lots of methods taking 
Rcpp::NumericVectors as arguments. Since I also call these methods from 
within C++, I would like to pass the arguments by 'const reference' and 
not by value. Thus, I wrote methods with the following signature:

Rcpp::List A::foo(const Rcpp::NumericVector& v);

This seemed to work well for several methods. Then, however, I wrote a 
constructor with signature
A::A(const Rcpp::NumericVector& v);
and exposed it via
.constructor<const Rcpp::NumericVector&>("test")

This code compiled without error, but unlike the previous methods, I got 
strange results. When called from R, the vector 'v' contains random 
values (like an uninitialized vector, or maybe an incorrectly casted 
object) within the constructor.

My questions therefore are:
1. Is this a bug or are reference arguments not intended/not safe to 
use/not allowed?
2. Is this unwanted behaviour restricted to constructors, or could it 
also happen for other methods of the class?
3. Does the copy constructor for Rcpp::NumericVector actually create a 
deep copy (duplicate elements)?

As always, I appreciate any assistance!

Best regards,
Peter


More information about the Rcpp-devel mailing list