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

Dirk Eddelbuettel edd at debian.org
Thu Oct 24 05:37:11 CEST 2013

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

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

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

Hope this helps,  Dirk

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

More information about the Rcpp-devel mailing list