<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br><div><div>Le 6 mai 2014 à 08:45, Florian Burkart <<a href="mailto:florian.burkart@gmail.com">florian.burkart@gmail.com</a>> a écrit :</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr"><div><div dir="ltr"><div style="font-family:arial,sans-serif;font-size:13px">Hi everyone (and Dirk),</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">
Second attempt on corrected email list.</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">I have been trying to extend Rcpp with my own wrap and as templates.</div>
<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">Two issues:</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">
1) I need to explicitly call wrap. Is that expected?</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">So for example I wrote this specialization:</div></div></div></div></blockquote><div><br></div><div>This is not a specialization, just another overload. You need to write a specialization, as in: </div><div><span style="font-family: arial, sans-serif; font-size: 13px;"><br></span></div><div><span style="font-family: arial, sans-serif; font-size: 13px;">namespace Rcpp{</span></div><div>t<font face="arial, sans-serif" size="2">emplate<> SEXP wrap<std::vector<</font> <span style="font-family: arial, sans-serif; font-size: small;">TimedOptDouble</span> <span style="font-family: arial, sans-serif; font-size: small;">> > (std::vector<TimedOptDouble> const& entries) { … }</span></div><div>}</div><br><blockquote type="cite"><div dir="ltr"><div><div dir="ltr"><div style="font-family:arial,sans-serif;font-size:13px"><div>template<> SEXP Rcpp::wrap(std::vector<TimedOptDouble> const& entries) {</div>
<div><span style="white-space:pre-wrap">  </span>std::vector<double> sec_times;</div><div><span style="white-space:pre-wrap">     </span>std::vector<double> doubles;</div><div><span style="white-space:pre-wrap">       </span>for(auto const& entry : entries)</div>
<div><span style="white-space:pre-wrap">  </span>{</div><div><span style="white-space:pre-wrap">                </span>sec_times.push_back(entry.GetTime().Seconds());</div><div><span style="white-space:pre-wrap">          </span>TimedOptDouble::OptDouble opt_double(entry.GetOptDouble());</div>
<div><span style="white-space:pre-wrap">          </span>if(opt_double)</div><div><span style="white-space:pre-wrap">                   </span>doubles.push_back(*opt_double);</div><div><span style="white-space:pre-wrap">          </span>else</div><div><span style="white-space:pre-wrap">                     </span>doubles.push_back(R_NaReal);</div>
<div><span style="white-space:pre-wrap">  </span>}</div><div><span style="white-space:pre-wrap">        </span>return List::create(</div><div><span style="white-space:pre-wrap">             </span>Named( "Time" ) = sec_times,</div>
<div><span style="white-space:pre-wrap">          </span>Named( "Value" ) = doubles);</div><div>}</div></div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">
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.</div></div></div></div></blockquote><blockquote type="cite"><div dir="ltr"><div><div dir="ltr"><div style="font-family:arial,sans-serif;font-size:13px"><br>
</div><div style="font-family:arial,sans-serif;font-size:13px">Unfortunately, when making use of this template, it doesn't work implicitly, but I need to explicitly call it.</div><div style="font-family:arial,sans-serif;font-size:13px">
<br></div><div style="font-family:arial,sans-serif;font-size:13px">So for example</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">SEXP GetSunPositions(SEXP a) {<br>
</div><div style="font-family:arial,sans-serif;font-size:13px"><span style="white-space:pre-wrap">        </span>std::vector<TimedOptDouble> sun_positions;<br></div><div style="font-family:arial,sans-serif;font-size:13px">
<span style="white-space:pre-wrap">     </span>...</div><div style="font-family:arial,sans-serif;font-size:13px"><span style="white-space:pre-wrap">        </span>return wrap(sun_positions);<br></div><div style="font-family:arial,sans-serif;font-size:13px">
}</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">works, where as</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">
return sun_positions;<br></div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">as last line doesn't. Am I doing something wrong here? I did do the declaration before including <Rcpp.h>.</div></div></div></div></blockquote><div><br></div><div>This should work if you return a <font face="arial, sans-serif" size="2">std::vector<</font> <span style="font-family: arial, sans-serif; font-size: small;">TimedOptDouble</span> <span style="font-family: arial, sans-serif; font-size: small;">> from your function, as in: </span></div><div><span style="font-family: arial, sans-serif; font-size: small;"><br></span></div><div><font face="arial, sans-serif" size="2">std::vector<</font> <span style="font-family: arial, sans-serif; font-size: small;">TimedOptDouble</span> <span style="font-family: arial, sans-serif; font-size: small;">> </span><font face="arial, sans-serif" size="2">GetSunPositions(SEXP a) { … }</font></div><br><blockquote type="cite"><div dir="ltr"><div><div dir="ltr"><div style="font-family:arial,sans-serif;font-size:13px">2) How to make as work for own types in containers</div><div style="font-family:arial,sans-serif;font-size:13px">
<br></div><div style="font-family:arial,sans-serif;font-size:13px">The other way around, one can return a std::vector<double> implicitly, but how do I return std::vector<MyType>? I tried to define</div><div style="font-family:arial,sans-serif;font-size:13px">
<br></div><div style="font-family:arial,sans-serif;font-size:13px"><span style="white-space:pre-wrap">      </span>template<> MyType as(SEXP);</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">
But that didn't help, e.g. I had to write my own</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px"><span style="white-space:pre-wrap"> </span>template<> std::vector<MyType> as(SEXP);</div></div></div></div></blockquote><div><br></div><div>This is the easiest way. A more general way would need you to express how to handle containers of MyType, but that requires defining some traits classes etc … not sure it is worth the effort. </div><div><br></div><div>But again, you’d need to actually write a specialization: <span style="font-family: arial, sans-serif; font-size: 13px;">template<> std::vector<MyType> as< </span><span style="font-family: arial, sans-serif; font-size: 13px;">std::vector<MyType> </span><span style="font-family: arial, sans-serif; font-size: 13px;">> (SEXP);</span></div><div><br></div><div>Romain</div><div><br></div><br><blockquote type="cite"><div dir="ltr"><div><div dir="ltr">
<div style="font-family:arial,sans-serif;font-size:13px">Thanks for help</div><div class="" style="font-family:arial,sans-serif;font-size:13px"><div id=":10y" class="" tabindex="0">
<img class="" src="https://mail.google.com/mail/u/0/images/cleardot.gif"></div></div>Florian<br></div></div>
</div>
_______________________________________________<br>Rcpp-devel mailing list<br><a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</blockquote></div><br></body></html>