<div dir="ltr">Thanks Dirk and Romain for your helpful replies. To follow up briefly...<div class="gmail_extra"><br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I'm defining a custom class, an object of which will need to survive<br>
across various calls back and forth between R and C++, so I plan to use<br>
the XPtr class to wrap a pointer. My question is, what are the<br>
advantages and disadvantages of using Rcpp vector classes (vs<br>
std::vector) for member variables? To be more concrete, I mean<br>
<br>
class Foo<br>
{<br>
private:<br>
   Rcpp::NumericVector bar;<br>
}<br>
<br>
vs<br>
<br>
class Foo<br>
{<br>
private;<br>
   std::vector<double> bar;<br>
}<br>
<br>
Are there garbage collection issues when these live inside an XPtr<Foo>?<br>
</blockquote>
<br></div>
No. An XPtr<Foo> will delete the object it points to, using Foo's destructor when it goes out of scope.<br></blockquote><div><br></div><div style>Sure. And the memory allocated to "bar" (if it's a NumericVector) will be protected from the garbage collector until the Foo object is deleted?</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I would argue against using external pointers directly when you can use modules and experience more type safety than with direct external pointers.<br>
<br>
But these (using external pointers and using modules) only make sense when you want to be able to hold a reference to your object at the R level, do you ?</blockquote><div><br></div><div style>I think I do... ;)  I need the object to hold state and not be deallocated between calls into the C++ code. I also want to allow for the possibility that multiple Foo objects exist, and are being operated on simultaneously. So holding a handle on the R side and passing it back to each C++ function that works with the object seems like the natural approach to me.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Are there speed advantages of std::vector<double> over<br>
Rcpp::NumericVector for general use? Any input would be welcome. Thanks<br>
in advance.<br>
</blockquote>
<br></div>
This is premature optimization. What you want to ask yourself is what are you going to do with "bar". If bar goes back and forth between the C++ and the R side, then NumericVector is your best candidate.<br></blockquote>
<div><br></div><div style>Agreed, but it's a consideration. I fully accept that the choice depends on the particular application, as you and Dirk both said. I was just wondering what the baseline performance difference (if any) might be.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
If bar is something internal to your class, then std::vector<> is fine and will give you a more complete interface, will grow efficiently, etc ...<br>
<br>
If you really want to have the best performing class for your application, you need to measure it.<br>
<br>
It is easy enough to make Foo a template and switch between the two in benchmarking:<br>
<br>
template <typename Container><br>
class Foo {<br>
<br>
private:<br>
        Container bar ;<br>
} ;<br>
<br>
Foo< std::vector<double> > f1;<br>
Foo< Rcpp::NumericVector > f2;<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Great work on Rcpp, by the way. I've been hearing very good things for<br>
quite some time, but wasn't sure if it was worth dusting off my slightly<br>
rusty C++ for. Suffice to say I think it was. The API is very clean and<br>
returning to the standard R API will be painful...!<br>
</blockquote>
<br></div>
Great. You don't need expert knowledge of C++ for Rcpp to be useful.</blockquote><div><br></div><div style>Sure, but I'm doing quite a bit in native code so there's little point in dropping competent C code for badly-written C++. Yes, I could mix them, but it's nice to be able to make the most of the tools available... :)</div>
<div style><br></div><div style>Regards,</div><div style>Jon</div><div style><br></div></div></div></div>