[Rcpp-devel] RcppModules with templated class

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


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 
In file included from 
In file included from 
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 

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,


PS Code on this branch:


with these 2 commits making the changes:


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


More information about the Rcpp-devel mailing list