Hi JJ,<div><br></div><div>Thank you for the reply. I would actually like to pass C++ functions to the C++ function foo(). To be more specific, the user will indicate in the R environment which of the two functions (fun1 or fun2) to pass to foo(). foo() will somehow use the chosen one accordingly. I suppose one option is to wrap foo() inside another function. The wrapper function can take input from the user from R, and then based on what the input is choose the appropriate function (fun1 or fun2). But the question of how to pass functions with different numbers of arguments still remains.  I wonder if you have any thoughts. Thank you again!</div>

<div><br></div><div>Best,</div><div>Xiao</div><div><br></div><div><br></div><div><br></div><div><br><div class="gmail_quote">On Sat, May 18, 2013 at 12:45 PM, JJ Allaire <span dir="ltr"><<a href="mailto:jj.allaire@gmail.com" target="_blank">jj.allaire@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><font face="arial,helvetica,sans-serif">Hi Xiao,</font><div><font face="arial,helvetica,sans-serif"><br></font></div><div>

<font face="arial,helvetica,sans-serif">The problem is that sourceCpp can only accept arguments of types that can be converted to R using Rcpp::as (this is detailed in the help topic for sourceCpp and in the Rcpp vignettes). Plain C function pointers aren't convertible in this fashion so the compilation fails.</font></div>


<div><font face="arial,helvetica,sans-serif"><br></font></div><div><font face="arial,helvetica,sans-serif">If your intention is to pass an R function to C++ you can however use the Rcpp::Function type in your signature. This is explained in more detail in this Rcpp Gallery article:</font></div>


<div><font face="arial,helvetica,sans-serif"><br></font></div><div><font face="arial,helvetica,sans-serif"><a href="http://gallery.rcpp.org/articles/r-function-from-c++/" target="_blank">http://gallery.rcpp.org/articles/r-function-from-c++/</a></font></div>


<div><font face="arial,helvetica,sans-serif"><br></font></div><div><font face="arial,helvetica,sans-serif">J.J.<br></font><br><div class="gmail_quote"><div><div class="h5">On Sat, May 18, 2013 at 1:31 PM, Xiao He <span dir="ltr"><<a href="mailto:praguewatermelon@gmail.com" target="_blank">praguewatermelon@gmail.com</a>></span> wrote:<br>


</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">Hi everyone, I have two questions regarding passing a function name as an argument.<div>

<br></div><div>(1). Suppose that I have a function foo() shown below. The function takes a NumericVector and a function pointer that points to a function that takes a NumericVector and returns a double. Note that the function pointer will only point to functions not exposed to R.</div>




<div><br></div><div><div><font face="courier new, monospace">double foo(NumericVector x, double (*f)(NumericVector x) ){</font></div><div><font face="courier new, monospace"><span style="white-space:pre-wrap">       </span>double output;</font></div>




<div><font face="courier new, monospace"><span style="white-space:pre-wrap">      </span>output=(*f)(x);</font></div><div><font face="courier new, monospace"><span style="white-space:pre-wrap"> </span>return output;</font></div>




<div><font face="courier new, monospace">}</font></div><div><br></div></div><div>When I try to compile it as is using sourceCpp(), it's fine, but when I add "// [[Rcpp::export]]" above the function definition, I get an error message:</div>




<div><br></div><div><div><font face="courier new, monospace">Error in sourceCpp("foo.cpp") : </font></div><div><font face="courier new, monospace">  Error 1 occurred building shared library.</font></div><div><font face="courier new, monospace">foo.cpp:229: error: expected initializer before ‘SEXP’</font></div>




</div><div><br></div><div>So I wonder how I can fix this mistake.</div><div><br></div><div><br></div><div>(2). Imagine a more complex scenario: suppose there are two functions available to be passed to foo(). But the two functions differ in the number of arguments each has (see fun1() and fun2() below). I wonder if there is any way to deal with this.</div>




<div><br></div><div><div><font face="courier new, monospace">double fun1(NumericVector x){</font></div><div><font face="courier new, monospace"><span style="white-space:pre-wrap">     </span>double total=0;</font></div>

<div><font face="courier new, monospace"><span style="white-space:pre-wrap">      </span>for(int i=0;i<x.size();i++)</font></div><div><font face="courier new, monospace"><span style="white-space:pre-wrap">          </span>total+=x(i);</font></div>




<div><font face="courier new, monospace"><span style="white-space:pre-wrap">      </span>return total/10;</font></div><div><font face="courier new, monospace">}</font></div><div><font face="courier new, monospace"><br>

</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">double fun2(NumericVector x, int n){</font></div><div><font face="courier new, monospace"><span style="white-space:pre-wrap">       </span>double total=0;</font></div>




<div><font face="courier new, monospace"><span style="white-space:pre-wrap">      </span>for(int i=0;i<x.size();i++)</font></div><div><font face="courier new, monospace"><span style="white-space:pre-wrap">          </span>total+=x(i)+add;</font></div>




<div><font face="courier new, monospace"><span style="white-space:pre-wrap">      </span>return total/n;</font></div><div><font face="courier new, monospace">}<span style="white-space:pre-wrap">        </span></font></div>

<div><span style="white-space:pre-wrap"><font face="courier new, monospace">      </font></span></div></div><div>Thank you in advance!</div><div><br></div><div><br></div><div>Best,</div><div>-Xiao</div><div>
<br>
</div>
<br></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><br></blockquote></div><br></div>
</blockquote></div><br></div>