<font face="arial,helvetica,sans-serif"><br></font>> // [[Rcpp::export]]<br>> int throwCpp(int nb) {<br>> BEGIN_RCPP<br>>   if (nb == 10)<br>>     Rcpp::stop("Unexpected condition occurred");<br>>   return nb*2; // Just for testing the normal case }<br>

> END_RCPP<br>> }<br><br>This doesn't quite work for a couple of reasons (one small one larger):<br><br>(1) END_RCPP assumes an SEXP return type so you'd actually need to code it like this:<br><br><font face="courier new, monospace">// [[Rcpp::export]]<br>

SEXP throwCpp(int nb) {<br>BEGIN_RCPP<br>   if (nb == 10)<br>      Rcpp::stop("Unexpected condition occurred");<br>   return Rcpp::wrap(nb*2);</font><div><font face="courier new, monospace">END_RCPP<br>
</font><div><font face="courier new, monospace">}</font></div><div><br></div><div>Or we could introduce a new variation of END_RCPP something like this:</div><div><br></div><div><font face="courier new, monospace">// [[Rcpp::export]]<br>
int throwCpp(int nb) {<br>BEGIN_RCPP<br>   if (nb == 10)<br>      Rcpp::stop("Unexpected condition occurred");<br>   return nb*2; </font><div><font face="courier new, monospace">END_RCPP_(0)<br></font><div><font face="courier new, monospace">}</font></div>
</div></div><div><br></div><div>(2) The more serious issue is that locating the END_RCPP inside the function will result in an Rf_error that does a longjmp over internal Rcpp code (bypassing C++ destructors in the process). The END_CPP construct really needs to sit right at the extern "C" boundary to be fully safe.</div>
<div><br></div><div><br></div><div>As Romain said, our aspiration is to make it possible to write C++ code for R without boilerplate scaffolding. We've got a couple of alternatives under discussion and I'm sure we'll be able to get this to work one way or another.</div>
<div><br></div><div>J.J.</div><div><br></div><div><br></div><div>
<br><div><br></div></div></div>