[Rcpp-devel] RcppModules with templated class

Dirk Eddelbuettel edd at debian.org
Mon Sep 1 14:27:01 CEST 2014


On 1 September 2014 at 10:48, Dr Gregory Jefferis wrote:
| 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 am not sure.  Rcpp Modules is another very nice, and very clever piece of
work by Romain. It does a number of tricks well. 

But in this case consider that
a) R itself only knows double so at some point both variants need to be
promoted up
b) Rcpp does a lot of magic for the C++ classes / member functions which
become callable by R BUT recall that R does not have type signatures.
So  foo(const double & x)  and  foo(const float & x)  look the same to R.

| 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 

Maybe, or maybe bite the bullet on this one now, and just typedef to one of
your two implementations?

Dirk

| 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
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list