[Rcpp-devel] operator<< issues

Dirk Eddelbuettel edd at debian.org
Mon Mar 19 12:36:10 CET 2018


On 19 March 2018 at 08:24, Iñaki Úcar wrote:
| Hi -- I hope my last email didn't hit the spam folder. :-)

It didn't but it is a little hard to say anything here.  Sometimes the
compiler needs help with disambiguation as you said.

Dirk
 
| Iñaki
| 
| 2018-03-14 18:51 GMT+01:00 Iñaki Úcar <i.ucar86 at gmail.com>:
| > Hi all,
| >
| > I'm not sure whether this is a bug or not, so I think this is the
| > right place to start with. Consider the following code:
| >
| > Rcpp::sourceCpp(code='
| >   #include <Rcpp.h>
| >   using namespace Rcpp;
| >
| >   // [[Rcpp::export]]
| >   void print_fun(Function x) {
| >     Rcout << x << std::endl;
| >   }
| >
| >   // [[Rcpp::export]]
| >   void print_env(Environment x) {
| >     Rcout << x << std::endl;
| >   }
| > ')
| >
| > print_fun(function() {})
| > print_env(environment())
| >
| > It compiles and the output from the functions are two addresses. So
| > far, so good. However, if we try the same for a data frame, the
| > compilation fails, so we need to define the operator<< as follows:
| >
| > Rcpp::sourceCpp(code='
| >   #include <Rcpp.h>
| >   using namespace Rcpp;
| >
| >   inline std::ostream& operator<<(std::ostream& out, const DataFrame& df) {
| >     out << "data.frame";
| >     return out;
| >   }
| >
| >   // [[Rcpp::export]]
| >   void print_df(DataFrame x) {
| >     Rcout << x << std::endl;
| >   }
| > ')
| >
| > print_df(data.frame(x=1))
| >
| > Now, it compiles and produces the output we defined. Once more, so
| > far, so good. Now the problem comes when we try to merge the two
| > examples above, that is:
| >
| > Rcpp::sourceCpp(code='
| >   #include <Rcpp.h>
| >   using namespace Rcpp;
| >
| >   inline std::ostream& operator<<(std::ostream& out, const DataFrame& df) {
| >     out << "data.frame";
| >     return out;
| >   }
| >
| >   // [[Rcpp::export]]
| >   void print_df(DataFrame x) {
| >     Rcout << x << std::endl;
| >   }
| >
| >   // [[Rcpp::export]]
| >   void print_fun(Function x) {
| >     Rcout << x << std::endl;
| >   }
| >
| >   // [[Rcpp::export]]
| >   void print_env(Environment x) {
| >     Rcout << x << std::endl;
| >   }
| > ')
| >
| > The compilation fails again due to an ambiguous overload for Function
| > and Environment types, so that we need to define the operator<< for
| > these classes too in order to disambiguate and fix this. I suppose it
| > may happen for other classes too. Is this... expected? Desirable? At
| > the very least, it is confusing from my point of view.
| >
| > Regards,
| > Iñaki
| _______________________________________________
| 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
-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list