Hi,

I just commited Rcpp::fixed_call to support uses of STL algorithms such 
as std::generate.

Here is an example that mimics :

 > lapply( 1:10, function(n) rnorm(10) )

using Rcpp API :

funx <- cfunction(signature(), '
        
        Language call( Function("rnorm"), 10 ) ;
        std::vector< std::vector<double> > result(10) ;
        std::generate(
                result.begin(), result.end(),
                fixed_call< std::vector<double> >(call)
                ) ;
        return wrap( result );
        ', Rcpp = TRUE, verbose = FALSE, includes = "using namespace Rcpp;" )

Note that fixed_call embeds the return type, in that case 
std::vector<double> which internally triggers an explicit as.


Here is the actual code of fixed_call:

template <typename OUT=SEXP>
class fixed_call {
public:
        typedef OUT result_type ;
        
        fixed_call( Language call_ ) : call(call_){}
        fixed_call( Function fun ) : call(fun){}
        
        OUT operator()(){
                return as<OUT>( call.eval() ) ;
        }
        
private:
        Language call ;
} ;

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/OIXN : raster images and RImageJ
|- http://tr.im/OcQe : Rcpp 0.7.7
`- http://tr.im/O1wO : highlight 0.1-5

_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to