<div dir="ltr"><div><div>Hi,<br></div>I have some questions regarding extending 'as' and 'wrap' for custom classes.<br><br></div><div>In my simple example I have declared a new class which has a single member. This member is of type NumericVector, i.e. it is an Rcpp class. I have written simple 'as' and 'wrap' extensions using non-intrusive extension. My simple example compiles and appears to work as expected.<br>
<br>My question is about the placement of #include <Rcpp.h>. In the Rcpp-extending vignette, it says that for non-intrusive extension, the #include <Rcpp.h> must appear after the specialisation otherwise the specialisation will not be seen by Rcpp types. However, if I do this compilation fails because the NumericVector member is not recognised by the compiler in the class definition.<br>
<br></div><div>As an experiment, I moved the #include <Rcpp.h> to before the class definition. My example then compiles and appears to run correctly. I was surprised because what I did appears to contradict the advice given the vignette.<br>
<br></div><div>So my questions are:<br></div><div>Should I be surprised that this example works even though it appears I have contradicted the advice in the vignette?<br></div><div>Although my example appears to work, because I have contradicted the advice in the vignette, is there now something bad lurking in the application. i.e. things appear fine with my simple example but will something blow up if I continue to develop my class?<br>
<br></div><div>My example cpp code is as follows:<br><br></div><div>// File: DummyClass_with_Rcpp_member_example.cpp<br></div><div>#include <RcppCommon.h><br>// If #include <Rcpp.h> not placed here then compilation fails<br>
#include <Rcpp.h><br><br>class DummyClass {<br>    public: <br>        Rcpp::NumericVector data;<br>};<br><br>namespace Rcpp {<br>    // non-intrusive extension via template specialisation<br>    template <> DummyClass as(SEXP dc_sexp);<br>
    // non-intrusive extension via template specialisation<br>    template <> SEXP wrap(const DummyClass &flq);<br>}<br><br>// Compilation fails if #include<Rcpp.h> placed here<br>//#include <Rcpp.h><br>
<br>// define template specialisations for as and wrap<br>namespace Rcpp {<br>    template <> DummyClass as(SEXP dc_sexp) {<br>    S4 dc_s4 = Rcpp::as<S4>(dc_sexp);<br>    DummyClass dc;<br>    dc.data = dc_s4.slot(".Data");<br>
        return dc;<br>    }<br><br>    template <> SEXP wrap(const DummyClass &dc) {<br>    Rcpp::S4 dc_s4("DummyClass");<br>    dc_s4.slot(".Data") = dc.data;<br>        return Rcpp::wrap(dc_s4);<br>
    }<br>}<br><br>// [[Rcpp::export]]<br>DummyClass test_DC_as_wrap(DummyClass dc, double multiplier){<br>    DummyClass new_dc;<br>    new_dc.data = dc.data;<br>    new_dc.data(0) = new_dc.data(0) * multiplier;<br>    return new_dc;<br>
}<br><br></div><div>The R code that runs the example is:<br><br>library(Rcpp)<br>sourceCpp("DummyClass_with_Rcpp_member_example.cpp")<br>setClass("DummyClass", representation(.Data = "numeric"))<br>
dc <- new("DummyClass")<br>dc@.Data <- 1:10<br>test_DC_as_wrap(dc, 4)<br><br></div><div>Yours<br><br></div><div>Finlay<br></div></div>