[Rcpp-devel] Template and non-template arguments in RCPP_RETURN_VECTOR

Romain François romain at r-enthusiasts.com
Wed Aug 20 18:15:44 CEST 2014


Hi, 

I guess RCPP_RETURN_VECTOR could be easily extended to support variadic number of arguments, using either variadic templates or variadic macros. 
https://github.com/RcppCore/Rcpp/blob/3ef9e662d89ebcfe71734675071568302bf10104/inst/include/Rcpp/macros/dispatch.h#L44

RCPP_RETURN_VECTOR is not that used as it looks kind of ugly and forces on you dispatch on all the SEXP types and sometimes you just want say INTSXP and REALSXP ...

Romain

Le 20 août 2014 à 14:30, Sven E. Templer <sven.templer at gmail.com> a écrit :

> To create a templated function one can use the macro RCPP_RETURN_VECTOR, e.g:
> 
> sourceCpp(code='
> #include <Rcpp.h>
> template <typename T>
> T index_template ( T X )
> {
>  Rcpp::IntegerVector I(1, 0);
>  return X[I];
> }
> // [[Rcpp::export]]
> SEXP index ( SEXP X )
> {
>  RCPP_RETURN_VECTOR(index_template, X);
> }
> ')
> index(letters)
> index(c(pi,2,1))
> 
> 
> Using a templated and a non-templated argument does not(?) allow use
> of the macro, e.g.:
> 
> sourceCpp(code='
> #include <Rcpp.h>
> template <typename T>
> T index2_template ( T X, Rcpp::IntegerVector & I )
> {
>  return X[I];
> }
> // [[Rcpp::export]]
> SEXP index2 ( SEXP X,  Rcpp::IntegerVector & I )
> {
>  switch(TYPEOF(X)) {
>    case STRSXP: return index2_template(Rcpp::Vector<STRSXP>(X), I); break;
>    case REALSXP: return index2_template(Rcpp::Vector<REALSXP>(X), I); break;
>    case INTSXP: return index2_template(Rcpp::Vector<INTSXP>(X), I); break;
>    default: Rf_error("Unsupported type.");
>  }
> }
> ')
> index2(c(pi,2,1),2)
> index2(letters,2)
> 
> Is there an easier way that I am missing, than to use switch/TYPEOF ...?
> Like RCPP_RETURN_VECTOR2(index_template, X, I).
> 
> Thank you for any hint,
> Sven.
> _______________________________________________
> 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



More information about the Rcpp-devel mailing list