-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow implicit conversion to Rcpp::RObject
#114
Comments
Hey, yes, it is definitely possible to add an implicit conversion like that. You'd just need to add a function like this: rcontainer ...
{
operator Rcpp::RObject () {
return Rcpp::as<Rcpp::RObject>(*this);
}
} However, I am unsure where to extend xtensor-r to support this. There is also a chance that Rcpp has an internal mechanism to do this (like they have for the SEXP casting, if you take a look at rcpp_extensions header). Maybe we can ask them? |
I know that
as summarized nicely in the first code block here But I guess this is technically I think this last example (repeated below) might only be working because Rcpp::RObject rray__exp_impl(const xt::rarray<rlogical>& x) {
xt::rarray<double> res = xt::exp(x);
return Rcpp::as<Rcpp::RObject>(res);
} I can ask someone if you aren't sure either |
This works because you're explicitly asking for this conversion. C++ allows one implicit conversion only. So implicitly you can convert to a SEXP like this: Now RObject has (most likely) a constructor from SEXP (which would require two conversions for implicit conversion) (rarray -> SEXP -> RObject). If you ask for it explicitly it would also work ( Does explicitly asking for Btw. I think rarray ... use the exact same mechanisms as RObject so in theory at least it shouldn't matter that your object stays an rarray. Out of curiosity, what benefits do you get from the RObject cast? |
Yea, both of these work: // [[Rcpp::depends(xtensor)]]
// [[Rcpp::plugins(cpp14)]]
#include <xtensor-r/rarray.hpp>
// [[Rcpp::export(rng = false)]]
Rcpp::RObject rray__exp_explicit(const xt::rarray<rlogical>& x) {
xt::rarray<double> res = xt::exp(x);
return Rcpp::RObject((SEXP) res);
}
// [[Rcpp::export(rng = false)]]
Rcpp::RObject rray__exp_direct(const xt::rarray<rlogical>& x) {
xt::rarray<double> res = xt::exp(x);
return Rcpp::RObject(res);
} As to why I'd do this, it's for the fact that A few functions, like the identity function
So I wanted to be able to do: template <typename T>
xt::rarray<T> rray__identity_impl(const xt::rarray<T>& x) {
return xt::operator+(x);
} But that won't work for logicals because the return type should be different than the input type. So I can do something like: template <typename T>
Rcpp::RObject rray__identity_impl(const xt::rarray<T>& x) {
return xt::operator+(x);
}
Rcpp::RObject rray__identity_impl(const xt::rarray<rlogical>& x) {
xt::rarray<int> res = xt::operator+(x);
return Rcpp::as<Rcpp::RObject>(res);
} |
Currently, something like this is allowed with no problems:
The (potentially safer) approach would be to return an
Rcpp::RObject
like this:But this gives me a "no viable conversion" error as it does not know how to convert to
RObject
fromxt::rarray<double>
. I can do something like this, but it is just a bit annoying:Do you know if its possible to add this implicit conversion?
PS: If you not aware,
RObject
s are wrappers aroundSEXP
s that Rcpp added that help automatically manage the underlying storage properties (they are also the base class that all core Rcpp classes inherit from). So you don't have to callPROTECT()
andUNPROTECT()
like you would have to do on the raw RSEXP
objectsThe text was updated successfully, but these errors were encountered: