[Rcpp-devel] Template and non-template arguments in RCPP_RETURN_VECTOR
Sven E. Templer
sven.templer at gmail.com
Wed Aug 20 14:30:12 CEST 2014
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.
More information about the Rcpp-devel
mailing list