[Rcpp-devel] Const Reference Arguments for class methods in Rcpp-Module
Dirk Eddelbuettel
edd at debian.org
Sat Apr 23 16:03:52 CEST 2011
Hi Peter,
On 23 April 2011 at 15:43, schattenpflanze at arcor.de wrote:
| 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)?
Bugs do happen. You could have found a corner case. Few people may have
pushed the const variants around.
So with that we would appreciate it if you poked around a bit more and maybe
created an actual replicable bug report with a small self-contained example.
Also, a personal note if I may: I think of classes like Rcpp::NumericVector
more as 'facilitators': they make it very easy to get R data structures (in
SEXP) 'down' to C++ to do work on them, and back again. When I want to do
more work in C++ itself, I prefer to use actual C++ data structures and would
use Rcpp::NumericVector to instantiate, say, a std::vector<double> which
other C++ classes can access. If I am doing 'matrix math', the Armadillo
classes work well; for general C++ work the STL is trusted.
Hope this helps, Dirk
--
Gauss has an Erdos number of -1.
-- #63 at http://www.gaussfacts.com
More information about the Rcpp-devel
mailing list