[Rcpp-devel] CRTP and Modules
FHRB Toledo
fernandohtoledo at gmail.com
Fri Jun 30 22:18:55 CEST 2017
Dear All,
I am trying to deploy a way to enable user extensions for a package that
uses Rcpp. In that sense, I have found a vignette from dplyr package
("Hybrid Evaluation") where it is used CRTP.
Thus, I am trying to reproduce e toy example of that to be exposed by Rcpp
Modules. Digging on the matter I found about the package RcppAnnoy which
have partially solved the issue. But, I still have a question: How to have
different implementations one for each Derived classes exposed by the Rcpp
Modules, specifically, how to expose the CRTP interface?
Following you can find What I did up to the moment. I also read an recent
thread in stackoverflow where Dirk mentioned RcppAnnoy but, I can't find
the way to expose the especializations.
Many thanks in advance for your help over that. By the way, I would be glad
with any comment or suggestion to fullfill the requirements for the CTRP.
even out of the scope of the Modules.
[CODE:]
#include <Rcpp.h>
template < class Derived >
class Base {
public :
Base< Derived > ( void ) { } ;
virtual ~Base< Derived > ( void ) { } ;
// CRTP interface
virtual double operation ( void )
{ return static_cast< Derived * >( this ) -> implementation ( ) ; }
private :
} ;
class Multiply : public Base< Multiply > {
public :
Multiply ( double, double ) ;
double implementation ( void ) ;
private :
double x, y ;
} ;
class Sum : public Base< Sum > {
public :
Sum ( double, double ) ;
double implementation ( void ) ;
private :
double x, y ;
} ;
Multiply::Multiply ( double x_, double y_ ) : x ( x_ ), y ( y_ ) { }
double Multiply::implementation ( void ) { return x * y ; }
Sum::Sum ( double x_, double y_ ) : x ( x_ ), y ( y_ ) { }
double Sum::implementation ( void ) { return x + y ; }
RCPP_EXPOSED_CLASS_NODECL( Base< Multiply > )
RCPP_EXPOSED_CLASS_NODECL( Base< Sum > )
RCPP_EXPOSED_CLASS( Multiply )
RCPP_EXPOSED_CLASS( Sum )
RCPP_MODULE( CRTP ) {
// needs templated parameter -- doesn't work
Rcpp::class_< Base >( "Base" )
// exposing interface specialization member function
.method( "operation", & Base::operation )
;
// exposing the class
Rcpp::class_< Multiply >( "Multiply" )
// explicit inheritance
// .derives< Base >( "Base" )
// exposing the default constructor
.constructor< double, double >( )
// exposing Base member functions
// .method( "operation", & Multiply::operation ) // doesn't work
;
// idem above
Rcpp::class_< Sum >( "Sum" )
// explicit inheritance
// .derives< Base >( "Base" )
.constructor< double, double >( )
.method( "operation", & Sum::implementation ) // works :) but it
isn't CRTP
;
}
Cheers,
FH
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20170630/480b124d/attachment.html>
More information about the Rcpp-devel
mailing list