[Rcpp-devel] Questions on extending Rcpp wrap and as with templates

Florian Burkart florian.burkart at gmail.com
Tue May 6 08:45:04 CEST 2014


Hi everyone (and Dirk),

Second attempt on corrected email list.

I have been trying to extend Rcpp with my own wrap and as templates.

Two issues:

1) I need to explicitly call wrap. Is that expected?

So for example I wrote this specialization:

template<> SEXP Rcpp::wrap(std::vector<TimedOptDouble> const& entries) {
std::vector<double> sec_times;
std::vector<double> doubles;
for(auto const& entry : entries)
{
sec_times.push_back(entry.GetTime().Seconds());
TimedOptDouble::OptDouble opt_double(entry.GetOptDouble());
if(opt_double)
doubles.push_back(*opt_double);
else
doubles.push_back(R_NaReal);
}
return List::create(
Named( "Time" ) = sec_times,
Named( "Value" ) = doubles);
}

First of all, this returns what I believe to be a Rcpp::List object, which
seems to be converted implicitly to a SEXP. This is the typical behaviour I
know.

Unfortunately, when making use of this template, it doesn't work
implicitly, but I need to explicitly call it.

So for example

SEXP GetSunPositions(SEXP a) {
std::vector<TimedOptDouble> sun_positions;
...
return wrap(sun_positions);
}

works, where as

return sun_positions;

as last line doesn't. Am I doing something wrong here? I did do the
declaration before including <Rcpp.h>.

2) How to make as work for own types in containers

The other way around, one can return a std::vector<double> implicitly, but
how do I return std::vector<MyType>? I tried to define

template<> MyType as(SEXP);

But that didn't help, e.g. I had to write my own

template<> std::vector<MyType> as(SEXP);

Thanks for help
Florian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140506/3df6a6ca/attachment.html>


More information about the Rcpp-devel mailing list