[Rcpp-devel] overloading = operator

Dirk Eddelbuettel edd at debian.org
Wed Dec 9 00:13:49 CET 2009


On 8 December 2009 at 21:52, Romain François wrote:
| On the subject of getting some convenience stuff into Rcpp, what about 
| overloading the = operator and do for example something like this :
|
| std::string& operator= ( const SEXP& robject  );
| std::vector<std::string>& operator= ( const SEXP& robject );
| 
| and so on. so that we could have .
| 
| SEXP foobar( SEXP foo, SEXP bar){
| 	std::string foo_string = foo ;
|   	// ...
| }
| 
| The implementations would have to take care of the TYPEOF's but it might 
| be a good way of doing it.

It would be good but I don't think it can be done just yet.  You can only
define operator for your classes, and SEXP isn't one.  Also, the signature
part matters to be sure that the right one gets invoked. Which is why I think
the following is your best bet and should work now:

    std::vector<std::string> foo = RcppStringVector(robject).stlVector();

I guess we could create a matching RcppString for a single value and this use:

    std::string foo = RcppString(robject).stdString();

but we already have essentially that via RcppParams::getStringValue("name").
Does that make sense?

Dirk


-- 
Three out of two people have difficulties with fractions.


More information about the Rcpp-devel mailing list