[Rcpp-devel] What are RcppExport, BEGIN_RCPP and END_RCPP?

Dirk Eddelbuettel edd at debian.org
Tue Oct 25 14:48:13 CEST 2011


On 25 October 2011 at 17:01, Slava Razbash wrote:
| Hello,
| 
| What are RcppExport, BEGIN_RCPP and END_RCPP?
| For example:
| RccpExport SEXP myFunction( SEXP myVar_s) {
| BEGIN_RCPP
| 
| END_RCPP
| }

Please their #define statements in RcppCommon.h and Rcpp/preprocessor.h.

RcppExport is a simple

   #define RcppExport extern "C"

needed as the R APIP requires a C function interface from .Call(), our
interface to R.

BEGIN_RCPP and END_RCPP are equally simple placeholders for the try/catch
blocks we recommend:

   #ifndef BEGIN_RCPP
   #define BEGIN_RCPP try{ 
   #endif 

   #ifndef VOID_END_RCPP
   #define VOID_END_RCPP } catch( std::exception& __ex__ ){ forward_exception_to_r( __ex__ ) ; } catch(...){ ::Rf_error( "c++ exception (unknown reason)" ) ; }
   #endif

   #ifndef END_RCPP
   #define END_RCPP VOID_END_RCPP return R_NilValue;
   #endif

| Are they related to extern "C" { } ?
| Further, suppose that I am writing a function that uses both Rcpp and
| direct access to the inside the SEXP objects, do I need to use extern
| "C" { } or has Rcpp done it for me?

You need 'extern "C"' on a function you want to call from R.  

You have the same access to SEXP objects as you do with the R API: plain old
C macros---but also a rich class structure in C++ that makes life a lot
easier.  You can use either or both with Rcpp.  We take nothing away from
the existing API, but do provide a (as we like to think: useful) extension.

Dirk

-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list