[Rcpp-devel] Passing template class as argument in Rcpp function

Subhomoy Ghosh subhomoy25 at gmail.com
Thu Mar 4 20:31:34 CET 2021


Thanks, for the solution. I tried what you suggested and it shows "no
matching constructor for initialization of 'Rcpp::XPtr<Distribution>'".
Below here is my attempt:

class Distribution {

public:
  template <typename T>
  class Uniform2 {

    public:
    Uniform2(T max_,mat D_) :
    max(max_), D(D_) {}

    ~Uniform2(){};

    T max;
    mat D;
 };
};

// [[Rcpp::export]]
XPtr<Distribution> getUniform(const mat &D, SEXP max,int type) {
  // create pointer to an Uniform object and
  // wrap it as an external pointer
  if(type==1) {
          Rcpp::XPtr<Distribution> ptr(new
Distribution::Uniform2<double>(as<double>(max), D ), true);
          return ptr;
  } else if(type==2) {
          Rcpp::XPtr<Distribution> ptr(new
Distribution::Uniform2<int>(as<int>(max), D ), true);
          return ptr;
   }
  // return the external pointer to the R side
  return Rcpp::XPtr<Distribution>(R_NilValue);
}

Any idea how this can be fixed?

@Dirk, Thanks! I am aware of the .Call(). I can go that route depending on
how it goes.




On Thu, Mar 4, 2021 at 11:56 AM Iñaki Ucar <iucar at fedoraproject.org> wrote:

> On Thu, 4 Mar 2021 at 17:39, Subhomoy Ghosh <subhomoy25 at gmail.com> wrote:
> >
> > Hi,
> >
> >   This is related to my post. My question is in the same spirit except
> for one additional complexity. How can one create a new instance of a
> template class object with a pointer on it and create an external pointer
> that can be further passed as an argument of a function? Here is a small
> example of my goal that will obviously not run and show "Use of class
> template Uniform2 requires template arguments".
>
> You can create a non-template parent class (e.g., Distribution) from
> which your template class derives. Then you just return a pointer to
> the parent. In [1] you'll see an example of this (Activity is the
> parent class; Seize, Release and the others are template classes that
> inherit from Activity). In your example, you can use something like
>
> Rcpp::XPtr<Distribution> ptr(new Uniform2<double>(as<double>(max), D ),
> true);
>
> [1] https://github.com/r-simmer/simmer/blob/master/src/activity.cpp
>
> Iñaki
>
> > // template class
> > template <typename T>
> > class Uniform2 {
> >
> > public:
> >   Uniform2(T max_,mat D_) :
> >   max(max_), D(D_) {}
> >
> >   ~Uniform2(){};
> >
> >   T max;
> >   mat D;
> > };
> >
> >
> > /// get function
> > // [[Rcpp::export]]
> > XPtr<Uniform2> getUniform(const mat &D, SEXP max,char type) {
> >   // create pointer to a template Uniform object and
> >   // wrap it as an external pointer
> >   if(type=='double') {
> >           Rcpp::XPtr<Uniform2> ptr(new Uniform2<double>(as<double>(max),
> D ), true);
> >           return ptr;
> >   } else if(type=='int') {
> >           Rcpp::XPtr<Uniform2> ptr(new Uniform2<int>(as<int>(max), D ),
> true);
> >           return ptr;
> >    }
> >   // return the external pointer to the R side
> >   return Rcpp::XPtr<Uniform2>(R_NilValue);
> > }
> >
> >
> >
> > //do something
> > // [[Rcpp::export]]
> > double test2(double z, XPtr<Uniform2> xp) {
> >
> >   double CC= z * xp->max;
> >
> >   return CC;
> >
> > }
> >
> > Thanks,
> > Subhomoy
> > _______________________________________________
> > Rcpp-devel mailing list
> > Rcpp-devel at lists.r-forge.r-project.org
> > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
>
>
> --
> Iñaki Úcar
>


-- 
Subhomoy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20210304/efd019ec/attachment-0001.html>


More information about the Rcpp-devel mailing list