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

2014-08-20 Thread Sven E. Templer
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::VectorSTRSXP(X), I); break;
case REALSXP: return index2_template(Rcpp::VectorREALSXP(X), I); break;
case INTSXP: return index2_template(Rcpp::VectorINTSXP(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@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


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

2014-08-20 Thread Dirk Eddelbuettel

On 20 August 2014 at 14:30, Sven E. Templer wrote:
| Is there an easier way that I am missing, than to use switch/TYPEOF ...?

No because R is a dynamically-typed language and we will only know at
run-time what you are passing down.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


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

2014-08-20 Thread Romain François
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.temp...@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::VectorSTRSXP(X), I); break;
case REALSXP: return index2_template(Rcpp::VectorREALSXP(X), I); break;
case INTSXP: return index2_template(Rcpp::VectorINTSXP(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@lists.r-forge.r-project.org
 https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel