<div dir="ltr">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:<div><br></div><div>class Distribution {<br><br>public:<span class="gmail-im" style="color:rgb(80,0,80)"><br>  template <typename T><br>  class Uniform2 {<br> <br>    public:<br>    Uniform2(T max_,mat D_) :<br>    max(max_), D(D_) {}<br> <br>    ~Uniform2(){};<br> <br>    T max;<br>    mat D;<br> };<br></span>};</div><div><br></div><div>// [[Rcpp::export]]<br>XPtr<Distribution> getUniform(const mat &D, SEXP max,int type) {<br>  // create pointer to an Uniform object and<span class="gmail-im" style="color:rgb(80,0,80)"><br>  // wrap it as an external pointer<br></span>  if(type==1) {<br>          Rcpp::XPtr<Distribution> ptr(new Distribution::Uniform2<double>(as<double>(max), D ), true);<br>          return ptr;<br>  } else if(type==2) {<br>          Rcpp::XPtr<Distribution> ptr(new Distribution::Uniform2<int>(as<int>(max), D ), true);<span class="gmail-im" style="color:rgb(80,0,80)"><br>          return ptr;<br>   }<br>  // return the external pointer to the R side<br></span>  return Rcpp::XPtr<Distribution>(R_NilValue);<br>}<br></div><div><br></div><div>Any idea how this can be fixed? </div><div><br></div><div>@Dirk, Thanks! I am aware of the .Call(). I can go that route depending on how it goes.</div><div><br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Mar 4, 2021 at 11:56 AM Iñaki Ucar <<a href="mailto:iucar@fedoraproject.org">iucar@fedoraproject.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Thu, 4 Mar 2021 at 17:39, Subhomoy Ghosh <<a href="mailto:subhomoy25@gmail.com" target="_blank">subhomoy25@gmail.com</a>> wrote:<br>
><br>
> Hi,<br>
><br>
>   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".<br>
<br>
You can create a non-template parent class (e.g., Distribution) from<br>
which your template class derives. Then you just return a pointer to<br>
the parent. In [1] you'll see an example of this (Activity is the<br>
parent class; Seize, Release and the others are template classes that<br>
inherit from Activity). In your example, you can use something like<br>
<br>
Rcpp::XPtr<Distribution> ptr(new Uniform2<double>(as<double>(max), D ), true);<br>
<br>
[1] <a href="https://github.com/r-simmer/simmer/blob/master/src/activity.cpp" rel="noreferrer" target="_blank">https://github.com/r-simmer/simmer/blob/master/src/activity.cpp</a><br>
<br>
Iñaki<br>
<br>
> // template class<br>
> template <typename T><br>
> class Uniform2 {<br>
><br>
> public:<br>
>   Uniform2(T max_,mat D_) :<br>
>   max(max_), D(D_) {}<br>
><br>
>   ~Uniform2(){};<br>
><br>
>   T max;<br>
>   mat D;<br>
> };<br>
><br>
><br>
> /// get function<br>
> // [[Rcpp::export]]<br>
> XPtr<Uniform2> getUniform(const mat &D, SEXP max,char type) {<br>
>   // create pointer to a template Uniform object and<br>
>   // wrap it as an external pointer<br>
>   if(type=='double') {<br>
>           Rcpp::XPtr<Uniform2> ptr(new Uniform2<double>(as<double>(max), D ), true);<br>
>           return ptr;<br>
>   } else if(type=='int') {<br>
>           Rcpp::XPtr<Uniform2> ptr(new Uniform2<int>(as<int>(max), D ), true);<br>
>           return ptr;<br>
>    }<br>
>   // return the external pointer to the R side<br>
>   return Rcpp::XPtr<Uniform2>(R_NilValue);<br>
> }<br>
><br>
><br>
><br>
> //do something<br>
> // [[Rcpp::export]]<br>
> double test2(double z, XPtr<Uniform2> xp) {<br>
><br>
>   double CC= z * xp->max;<br>
><br>
>   return CC;<br>
><br>
> }<br>
><br>
> Thanks,<br>
> Subhomoy<br>
> _______________________________________________<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" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
<br>
<br>
<br>
-- <br>
Iñaki Úcar<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr">Subhomoy</div></div>