[Rcpp-devel] RcppModules with templated class

Dr Gregory Jefferis jefferis at mrc-lmb.cam.ac.uk
Mon Sep 1 11:48:59 CEST 2014


Hello,

I have a more or less complete Rcpp(Eigen) dependent package that 
exposes a pair of C++ classes to R using RcppModules;the C++ classes 
(which implement k nearest neighbour search trees) differ only in their 
scalar storage type (float vs double) and are called WKNND and WKNNF. 
For future flexibility/maintenance I would have like to convert the C++ 
classes to templated code. Is it possible to use RcppModules to expose 
template classes? I know that Rcpp in general can be used with template 
classes, but the RcppModules manual does not seem to mention this.

I have tried to implement this by simple translation of my existing code 
to a single WKNN class and then defining types for  WKNND and WKNNF 
(i.e. double and float). However this falls over with errors/notes 
indicating that the automatic wrap/conversion of Eigen types is failing 
to pick up the typedef. Error:

In file included from WKNND.cpp:1:
In file included from 
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include/Rcpp.h:27:
In file included from 
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include/RcppCommon.h:169:
In file included from 
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include/Rcpp/as.h:25:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include/Rcpp/internal/Exporter.h:31:28: 
error: no matching constructor for initialization of 'WKNN<double>'
                     Exporter( SEXP x ) : t(x){}
                                          ^ ~
and notes like:

./WKNN.h:14:8: note: candidate constructor (the implicit copy 
constructor) not viable: cannot convert argument of incomplete type 
'SEXP' (aka 'SEXPREC *') to 'const WKNN<double>'
struct WKNN {
        ^
./WKNN.h:16:3: note: candidate constructor not viable: cannot convert 
argument of incomplete type 'SEXP' (aka 'SEXPREC *') to 'const 
Eigen::Map<Eigen::Matrix<double, Dynamic, Dynamic> >'
   WKNN(const Eigen::Map<Eigen::Matrix<T, Dynamic, Dynamic> > data, bool 
buildtree=true);
   ^

So is there a way round this? Do I have to write an explicit wrap 
function? Do I have to give up on using RcppModules altogether? Any 
advice or pointers to a package already doing this would be much 
appreciated. And of course big thanks to Dirk, Romain and Douglas et al 
for these great packages.

Best wishes,

Greg.

PS Code on this branch:

https://github.com/jefferis/nabor/tree/feature/template-WKNN

with these 2 commits making the changes:

https://github.com/jefferis/nabor/commit/fa85ddae5187f1e93801d7ec36d1d78923384cc6
https://github.com/jefferis/nabor/commit/9ff93e63023b2f6d9c7ddad54fb7d1f503685b86


--
Gregory Jefferis, PhD
Division of Neurobiology
MRC Laboratory of Molecular Biology
Francis Crick Avenue
Cambridge Biomedical Campus
Cambridge, CB2 OQH, UK

http://www2.mrc-lmb.cam.ac.uk/group-leaders/h-to-m/g-jefferis
http://jefferislab.org
http://flybrain.stanford.edu


More information about the Rcpp-devel mailing list