[Rcpp-devel] Using lbfgsb with a Rcpp::List.

Darren Cook darren at dcook.org
Mon Nov 21 01:27:32 CET 2011


> example from help(optim), but was wondering if anyone could help me
> passing an Rcpp::List to the minimisation function....
> I don't know how to get around the void type of the function input,

double fr(int n, double *par, void *ex) {
	double x1 = par[0];
	double x2 = par[1];
//	double blah = Rcpp::as<double>(ex["numbers"]);
...


void* means a pointer to anything. So if you know it is an Rcpp::List*,
you'd do:
   Rcpp::List *p=static_cast<Rcpp::List>(ex);
   double blah = Rcpp::as<double>(*p["numbers"]);

Note the *p to dereference the pointer. This version might also work and
be more readable:
   Rcpp::List &TheList=*(static_cast<Rcpp::List>(ex));
   double blah = Rcpp::as<double>(TheList["numbers"]);

Both of them untested.

As Dirk is saying, using void* is C-style code. C++ style would use
templates and functors to be both safer and slightly quicker.

Darren

-- 
Darren Cook, Software Researcher/Developer

http://dcook.org/work/ (About me and my work)
http://dcook.org/blogs.html (My blogs and articles)


More information about the Rcpp-devel mailing list