<div dir="ltr"><span style="font-family:arial,sans-serif;font-size:13px">Hi Romain,</span><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">You are correct.</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 changed to specializations as below.</div><div style="font-family:arial,sans-serif;font-size:13px">
<br></div><div style="font-family:arial,sans-serif;font-size:13px">However, as to the return type, are you saying the header file then also becomes</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">
<font face="arial, sans-serif">RcppExport</font><font face="arial, sans-serif"> std::vector<</font> TimedOptDouble > <font face="arial, sans-serif">GetSunPositions(SEXP a)</font><br></div><div style="font-family:arial,sans-serif;font-size:13px">
<font face="arial, sans-serif"><br></font></div><div style="font-family:arial,sans-serif;font-size:13px"><font face="arial, sans-serif">instead of </font></div><div style="font-family:arial,sans-serif;font-size:13px"><font face="arial, sans-serif"><br>
</font></div><div style="font-family:arial,sans-serif;font-size:13px"><font face="arial, sans-serif">RcppExport</font><font face="arial, sans-serif"> SEXP</font> <font face="arial, sans-serif">GetSunPositions(SEXP a)</font><font face="arial, sans-serif"><br>
</font></div><div style="font-family:arial,sans-serif;font-size:13px"><font face="arial, sans-serif"><br></font></div><div style="font-family:arial,sans-serif;font-size:13px"><font face="arial, sans-serif">? Or do I leave the header unchanged and just change the return type of the implementation?</font></div>
<div style="font-family:arial,sans-serif;font-size:13px"><font face="arial, sans-serif"><br></font></div><div style="font-family:arial,sans-serif;font-size:13px"><font face="arial, sans-serif"><br></font></div><div style="font-family:arial,sans-serif;font-size:13px">
<font face="arial, sans-serif">In terms of the second point, I use OptDouble a lot (e.g. boost::optional<double>), which converts to a double with</font></div><div style="font-family:arial,sans-serif;font-size:13px">
<font face="arial, sans-serif"><br></font></div><div style="font-family:arial,sans-serif;font-size:13px"><font face="arial, sans-serif"><div>double ToDouble(OptDouble const& in) {</div><div><span style="white-space:pre-wrap">       </span>return in ? *in : R_NaReal;</div>
<div>}</div><div><br></div><div>unfortunately that means that all the nice semantics available to me in Rcpp, e.g. Named("Results")=std::list, or std::vector, or std::vector<std::vector > > have all become unavailable as I always use OptDouble instead of double.</div>
<div><br></div><div>Is it really that much hassle for me to add boost::optional<double> to the internal handling?</div><div><br></div><div>Thank you</div><span class=""><font color="#888888">Florian</font></span></font></div>
<div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, May 6, 2014 at 9:10 AM, Romain Francois <span dir="ltr"><<a href="mailto:romain@r-enthusiasts.com" target="_blank">romain@r-enthusiasts.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><div>Le 6 mai 2014 à 08:45, Florian Burkart <<a href="mailto:florian.burkart@gmail.com" target="_blank">florian.burkart@gmail.com</a>> a écrit :</div>
<div class=""><br><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><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">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 class=""><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><div>This should work if you return a <font face="arial, sans-serif">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">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">GetSunPositions(SEXP a) { … }</font></div>
<div class=""><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><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 class=""><div dir="ltr"><div><div dir="ltr">
<div style="font-family:arial,sans-serif;font-size:13px">Thanks for help</div><div style="font-family:arial,sans-serif;font-size:13px"><div>
<img src="https://mail.google.com/mail/u/0/images/cleardot.gif"></div></div>Florian<br></div></div>
</div></div>
_______________________________________________<br>Rcpp-devel mailing list<br><a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br><a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></blockquote>
</div><br></div></blockquote></div><br></div></div>