<div dir="ltr"><br><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><a href="http://www.keittlab.org/" target="_blank">http://www.keittlab.org/</a></div></div></div>
<br><div class="gmail_quote">On Wed, Mar 14, 2018 at 12:51 PM, Iñaki Úcar <span dir="ltr"><<a href="mailto:i.ucar86@gmail.com" target="_blank">i.ucar86@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi all,<br>
<br>
I'm not sure whether this is a bug or not, so I think this is the<br>
right place to start with. Consider the following code:<br>
<br>
Rcpp::sourceCpp(code='<br>
  #include <Rcpp.h><br>
  using namespace Rcpp;<br>
<br>
  // [[Rcpp::export]]<br>
  void print_fun(Function x) {<br>
    Rcout << x << std::endl;<br>
  }<br>
<br>
  // [[Rcpp::export]]<br>
  void print_env(Environment x) {<br>
    Rcout << x << std::endl;<br>
  }<br>
')<br>
<br>
print_fun(function() {})<br>
print_env(environment())<br>
<br>
It compiles and the output from the functions are two addresses. So<br>
far, so good. However, if we try the same for a data frame, the<br>
compilation fails, so we need to define the operator<< as follows:<br>
<br>
Rcpp::sourceCpp(code='<br>
  #include <Rcpp.h><br>
  using namespace Rcpp;<br>
<br>
  inline std::ostream& operator<<(std::ostream& out, const DataFrame& df) {<br>
    out << "data.frame";<br>
    return out;<br>
  }<br>
<br>
  // [[Rcpp::export]]<br>
  void print_df(DataFrame x) {<br>
    Rcout << x << std::endl;<br>
  }<br>
')<br>
<br>
print_df(data.frame(x=1))<br>
<br>
Now, it compiles and produces the output we defined. Once more, so<br>
far, so good. Now the problem comes when we try to merge the two<br>
examples above, that is:<br>
<br>
Rcpp::sourceCpp(code='<br>
  #include <Rcpp.h><br>
  using namespace Rcpp;<br>
<br>
  inline std::ostream& operator<<(std::ostream& out, const DataFrame& df) {<br>
    out << "data.frame";<br>
    return out;<br>
  }<br>
<br>
  // [[Rcpp::export]]<br>
  void print_df(DataFrame x) {<br>
    Rcout << x << std::endl;<br>
  }<br>
<br>
  // [[Rcpp::export]]<br>
  void print_fun(Function x) {<br>
    Rcout << x << std::endl;<br>
  }<br>
<br>
  // [[Rcpp::export]]<br>
  void print_env(Environment x) {<br>
    Rcout << x << std::endl;<br>
  }<br>
')<br>
<br>
The compilation fails again due to an ambiguous overload for Function<br>
and Environment types, so that we need to define the operator<< for<br>
these classes too in order to disambiguate and fix this. I suppose it<br>
may happen for other classes too. Is this... expected? Desirable? At<br>
the very least, it is confusing from my point of view.<br></blockquote><div><br></div><div>Why not something like:</div><div><br></div><div><div>Rcpp::sourceCpp(code='</div><div>  #include <Rcpp.h></div><div>   using Rcpp::Rcout;</div><div>                </div><div>   // [[Rcpp::export]]</div><div>   void print_addr(SEXP x){</div><div>                Rcout << static_cast<void*>(x) << std::endl;</div><div> }')</div></div><div><br></div><div>I'm not sure why one would expect Rcpp types to automatically yield a pointer appropriate for printing.</div><div><br></div><div>THK</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
Regards,<br>
Iñaki<br>
______________________________<wbr>_________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-<wbr>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-<wbr>project.org/cgi-bin/mailman/<wbr>listinfo/rcpp-devel</a></blockquote></div><br></div></div>