<div dir="ltr">Hi,<div><br><div> <font face="arial, sans-serif"> This is related to my <a href="https://stackoverflow.com/questions/66142449/passing-class-as-argument-in-rcpp-function?noredirect=1#comment116942069_66142449" style="">post</a>. My question is in the same spirit except for one additional complexity. How can one create<span style="color:rgb(36,39,41)"> a new instance of a <b>template</b> 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". </span></font></div><div><font color="#242729" face="arial, sans-serif"><br></font></div><div><font color="#242729" face="arial, sans-serif">// template class </font></div><div>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><div><br></div><div><br></div><div>/// get function</div><div>// [[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></div><div><br></div><div><br></div><div><br></div><div>//do something</div><div>// [[Rcpp::export]]<br>double test2(double z, XPtr<Uniform2> xp) {<br>  <br>  double CC= z * xp->max;<br>  <br>  return CC;<br>  <br>}<br></div><div><br></div><div>Thanks,</div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">Subhomoy</div></div></div></div></div>