[Rcpp-devel] Dispatching based on SEXPTYPE

Dirk Eddelbuettel edd at debian.org
Thu Nov 22 17:04:47 CET 2012


On 22 November 2012 at 16:42, Romain Francois wrote:
| Le 22/11/12 16:25, Hadley Wickham a écrit :
| >> #define DISPATCH_METHOD(method, x)  \
| >>    switch( TYPEOF(x) ){          \
| >>      case REALSXP:                   \
| >>        return method<REALSXP>(x);    \
| >>      case INTSXP:                    \
| >>        return method<INTSXP>(x);     \
| >>      case STRSXP:                    \
| >>        return method<STRSXP>(x);     \
| >>      case LGLSXP:                    \
| >>        return method<LGLSXP>(x);     \
| >>      default:                        \
| >>        Rf_error("Unsupported type"); \
| >>        return x;                     \
| >>    }
| >
| > Why do we need a macro here? Is it not possible to pass in method as a
| > function?  It seems like there must be a more elegant way to do
| > dynamic dispatch.
| >
| > Hadley
| 
| Unfortunately, there is not. The issue is that we don't know the type of 
| x at compile type, so we have to do this switch dance and trigger 
| explicit instanciations of all method<> overloads.

It's one of those times were we get reminded that using C has limitations. 

To the compiler SEXP is just one type, and the dispathing on the 'union type'
is done at run-time, not compile time as one could via templates.
 
| There is no other way I know about.

Switch to Andrew Runnall's CXXR :)

Dirk

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


More information about the Rcpp-devel mailing list