Message-ID: <4EC99AF4.9020701@dcook.org>
Date: 2011-11-21T00:27:32Z
From: Darren Cook
Subject: [Rcpp-devel] Using lbfgsb with a Rcpp::List.
In-Reply-To: <D1BB26C3-FA21-48D0-81C0-9F0C9044C653@gmail.com>
> 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)