[Rcpp-devel] Extending Rcpp::wrap from the Extending Rcpp vignette

Douglas Bates bates at stat.wisc.edu
Mon Jun 21 18:01:37 CEST 2010


On Mon, Jun 21, 2010 at 1:39 AM, Romain Francois
<romain at r-enthusiasts.com> wrote:
> Le 20/06/10 18:27, Douglas Bates a écrit :
>>
>> I'm confused about where the Rcpp.h file is included when using
>> "intrusive" extension of Rcpp::wrap as described in section 2.1 of the
>> vignette "Extending Rcpp".
>>
>> I would like to define the SEXP operator for the C++ classes
>> corresponding to the Matrix package S4 classes.  These are declared in
>> lme4a/src/MatrixNs.h
>>
>> It appears to me from the vignette that I need to include RcppCommon.h
>> then declare my classes and the operator member SEXP() then include
>> Rcpp.h
>>
>> The problem is that my classes have members that are declared through
>> Rcpp.h, so, for example, I need declarations of Rcpp::List,
>> Rcpp::NumericVector, etc. to be able to declare my classes.
>>
>> If it is the case that I must declare my classes and their operator
>> SEXP() member before including Rcpp.h, should I use forward
>> declarations of the various Rcpp classes that are used in my classes?
>>
>> I hope the question is clear.  If not, I will provide sample code.
>>
>> This template stuff is very powerful but very confusing.
>
> Indeed.
>
>
> This is not something that has been stressed too much so far. I think these
> options might work :
>
> - as you say: forward declare Rcpp classes you intend to use. so something
> in that order:
>
> #include <RcppCommon.h>
>
> // forward declarations of Rcpp classes
>
> // include headers for your classes here
>
> #include <Rcpp.h>
>
>
> however, perhaps you wish to use classes that are generated by templates:
> NumericVector, etc ...

> In that case forward declaring might not work as it might trip up the
> compiler because, e.g NumericVector is just defined by a typedef:

> typedef Vector<REALSXP> NumericVector ;

> Using Vector<REALSXP> in your headers should work if the Vector template is
> predeclared : I might need to interfere here.

I did try the forward declarations including the typedef and was
unsuccessful.  It appears that more information about the class is
needed than just a forward declaration.

I'll try the other version later today.

> - the other way, which is probably easier, is to forward declare your
> classes and their wrap before you include Rcpp.h
>
> // forward decl
> class Foo ;
>
> // needed for the definition of wrap we want to specialize
> #include <RcppCommon.h>
>
> namespace Rcpp{
>        // forward decl
>        template<> SEXP wrap( const& Foo ) ;
> }
>
> #include Rcpp.h
>
> // real decl
> class Foo {
> public:
>        Foo( int n ) : vec(n){} ;
>        IntegerVector vec ;
> } ;
>
> namespace Rcpp{
>        template<> SEXP (const& Foo w){
>                return w.vec ;
>        }
> }
>
>
>
> In any case, we would like to hear back from your experience, so that we can
> improve the document.
>
> It might be possible to move more things into RcppCommon.h so that Rcpp
> takes care of forward declarations.
>
>
> Romain
>
> --
> Romain Francois
> Professional R Enthusiast
> +33(0) 6 28 91 30 30
> http://romainfrancois.blog.free.fr
> |- http://bit.ly/98Uf7u : Rcpp 0.8.1
> |- http://bit.ly/c6YnCi : graph gallery collage
> `- http://bit.ly/bZ7ltC : inline 0.3.5
>
>


More information about the Rcpp-devel mailing list