<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/passing-user-supplied-c-functions-with-rcppxptrutils/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://gallery.rcpp.org//articles/passing-cpp-function-pointers-rcppxptrutils/"> Rcpp Gallery</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Sitting on top of R’s external pointers, the <code>RcppXPtr</code> class provides<br>
a powerful and generic framework for<br>
<a rel="nofollow" href="http://gallery.rcpp.org/articles/passing-cpp-function-pointers/" target="_blank">Passing user-supplied C++ functions</a><br>
to a C++ backend. This technique is exploited in the<br>
<a rel="nofollow" href="https://cran.r-project.org/package=RcppDE" target="_blank">RcppDE</a> package, an<br>
efficient C++ based implementation of the<br>
<a rel="nofollow" href="https://cran.r-project.org/package=DEoptim" target="_blank">DEoptim</a> package that<br>
accepts optimisation objectives as both R and compiled functions (see<br>
<code>demo(&quot;compiled&quot;, &quot;RcppDE&quot;)</code> for further details). This solution has a<br>
couple of issues though:</p>
<ol>
<li>Some repetitive scaffolding is always needed in order to bring the <code>XPtr</code> to R space.</li>
<li>There is no way of checking whether a user-provided C++ function<br>
complies with the internal signature supported by the C++ backend,<br>
which may lead to weird runtime errors.</li>
</ol>
<h2>Better <code>XPtr</code> handling with RcppXPtrUtils</h2>
<p>In a nutshell, RcppXPtrUtils provides functions for dealing with these<br>
two issues: namely, <code>cppXPtr</code> and <code>checkXPtr</code>. As a package author,<br>
you only need to 1) import and re-export <code>cppXPtr</code> to compile code and<br>
transparently retrieve an <code>XPtr</code>, and 2) use <code>checkXPtr</code> to internally<br>
check function signatures.</p>
<p><code>cppXPtr</code> works in the same way as <code>Rcpp::cppFunction</code>, but instead of<br>
returning a wrapper to directly call the compiled function from R, it<br>
returns an <code>XPtr</code> to be passed to, unwrapped and called from C++. The<br>
returned object is an R’s <code>externalptr</code> wrapped into a class called<br>
<code>XPtr</code> along with additional information about the function signature.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>RcppXPtrUtils</span><span>)</span><span>
</span><span>ptr</span><span> </span><span>&lt;-</span><span> </span><span>cppXPtr</span><span>(</span><span>&quot;double foo(int a, double b) { return a + b; }&quot;</span><span>)</span><span>
</span><span>class</span><span>(</span><span>ptr</span><span>)</span></code></pre>
</figure>
<pre>
[1] &quot;XPtr&quot;
</pre>
<figure>
<pre><code data-lang="r"><span>ptr</span></code></pre>
</figure>
<pre>
'double foo(int a, double b)' </pre>