[Rcpp-devel] overloading = operator

Romain François francoisromain at free.fr
Wed Dec 9 07:36:25 CET 2009


On 12/09/2009 12:13 AM, Dirk Eddelbuettel wrote:
>
> 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.

Ah. I thought you could define friend operators for other classes. 
nevermind.

> 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?

yep.

> Dirk


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/Gq7i : ohloh
|- http://tr.im/FtUu : new package : highlight
`- http://tr.im/EAD5 : LondonR slides



More information about the Rcpp-devel mailing list