[Rcpp-devel] Exporter.h cannot convert 'SEXP' to 'const std::vector<double>*' in initialization

Jean-Michel.Perraud at csiro.au Jean-Michel.Perraud at csiro.au
Thu Oct 24 13:42:07 CEST 2013


Hi Dirk,
Thank you for the sound advice to simplify and test with cppInline. That indeed helps check what works or not for as<> and wrap<>.
Re-reading with a clearer head your book section on what can be wrapped by a call to _class.method(...), I figured out that I could use something like follows
void PlaySimEx(SimulationExport* simul, const char* name, Rcpp::NumericVector values) 
{
	const numvec v = Rcpp::as<numvec>(values);
	simul->Play(name, &v);
}
All seems to work as expected.
I think I have a proof of concept C++/Rcpp wrapper generator for Fortran 2003/2008; I'll try to share it on github in a few weeks.

________________________________________
From: Dirk Eddelbuettel [edd at debian.org]
Sent: Thursday, October 24, 2013 2:37 PM
To: Perraud, Jean-Michel (CLW, Black Mountain)
Cc: rcpp-devel at lists.r-forge.r-project.org
Subject: Re: [Rcpp-devel] Exporter.h cannot convert 'SEXP' to 'const std::vector<double>*' in initialization

Salut Jean-Michel,

On 23 October 2013 at 04:12, Jean-Michel.Perraud at csiro.au wrote:
| Hi,
|
| This has to be a basic question, but I cannot figure out what I am missing nor see quite similar pre-existing posts.
|
| I am trying to create an Rcpp module around a class (generated C++ proxy class). The environment is Rcpp 0.10.5 on Windows, R 3.0.2 x64 and RTools 3.0.
|
| R CMD INSTALL spits the dummy at compilation(?) time when the Exporter things kick in, with the message " cannot convert 'SEXP' to 'const std::vector<double>*' in initialization" on the 'Play' method as per below.

This is tricky. In essence, the template 'magic' needs to have a match for each
of types you supply.  And you get a failure here.

That can mean two things (at least).  We may have bug and something that
should work does not work.  Or you requested something that simply is not
implemented.

As you get more experienced with this, you learn to differentiate the case
more quickly. In a nutshell, it is the second case here as you wished
something was implemented that simply isn't.

My preferred approach is too simplify and to use inline or attributes to try
something simpler.

Eg these work (first is a little nonsensical):

R> cppFunction("const std::vector<double> foo() { std::vector<double> x(3); return(x); }")
R> foo()
[1] 0 0 0
R> cppFunction("std::vector<double> foo2() { std::vector<double> x(3); return(x); }")
R> foo2()
[1] 0 0 0
R>

But as soon as you add a '*' for pointer to vector (which is an odd notion
anyway, you'd rather use a reference (but then you can't in a return value))
it blows up.

So in short, your code had a design issue.  Make it

   void Play(const char* name, const std::vector<double> & values);

and you should be fine:

R> cppFunction("int foo3(const std::vector<double> & x) { return(x.size()); }")
R> foo3(rnorm(3))
[1] 3
R> cppFunction("int foo4(std::vector<double> & x) { return(x.size()); }")
R> foo4(rnorm(3))
[1] 3
R> cppFunction("int foo5(std::vector<double> x) { return(x.size()); }")
R> foo5(rnorm(3))
[1] 3
R>

Hope this helps,  Dirk



--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list