Dear Rcpp-list,
I'm trying to expose multiple constructors of a C++ class to R using
modules. Here is an example of what I want to do based on the Uniform
example
===Begin: example
library('inline')
library('Rcpp')
test_code <-'
using namespace Rcpp;
class Uniform {
public:
Uniform(double min_, double max_) : min(min_), max(max_) {}
Uniform(double max_, std::string dummy_) : min(0.0), max(max_)
{ Rprintf("%s\\n", dummy_.c_str()); }
Uniform(double max_ ) : min(0.0), max(max_) {}
NumericVector draw(int n) const {
RNGScope scope;
return runif( n, min, max );
}
double min, max;
};
double range( Uniform* w) {
return w->max - w->min;
}
RCPP_MODULE(unif_module) {
class_<Uniform>( "Uniform" )
.constructor<double,double>()
.constructor<double,std::string>()
.constructor<double>()
.field( "min", &Uniform::min )
.field( "max", &Uniform::max )
.method( "draw", &Uniform::draw )
.method( "range", &range )
;
}
'
fx <- cxxfunction( signature(), "" , include = test_code, plugin = "Rcpp" )
unif_module <- Module( "unif_module", getDynLib(fx) )
show( Uniform )
u1 <- new( Uniform, 0, 10 )
u1$min
u1$max
u1$range()
u1$draw( 10L )
u2 <- new( Uniform, 10, "test" )
u2$min
u2$max
u2$range()
u2$draw( 10L )
u3 <- new( Uniform, 10 )
u3$min
u3$max
u3$range()
u3$draw( 10L )
===End: example
Compilation works fine (on Windows using RTools, Rcpp_0.9.4.1,
inline_0.3.8), but the R code cannot distinguish between two
constructors with the same number of arguments, but with different
types for the arguments and always calls Uniform(double, double). The
output I get is as follows:
Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) :
not compatible with REALSXP
Does anyone know of a workaround for this?
Many thanks in advance!
Jelmer