Hi,
I have just discovered an extremely strange behaviour in conversion of
an R logical to a C++ bool. Consider the minimal example:
--- snip ---
library(Rcpp)
library(inline)
## this compiles and loads fine, but is wrong:
wrong <- cxxfunction(signature(x="list") , '
List input(x);
const bool a = input[0];
return wrap(a);
' , plugin = "Rcpp")
wrong(list(TRUE))
wrong(list(FALSE))
## this is correct:
correct <- cxxfunction(signature(x="list") , '
List input(x);
const bool a = as<bool>(input[0]);
return wrap(a);
' , plugin = "Rcpp")
correct(list(TRUE))
correct(list(FALSE))
--- snap ----
In Rcpp 0.8.9 wrong(list(FALSE)) == TRUE, which is quite surprising to
the user...
Sorry if this already a known feature, but then I would be curious if
this behaviour is really wished?
Thanks in advance,
best regards
Daniel
[Rcpp-devel] strange bool conversion?
3 messages · Daniel Sabanés Bové, Romain Francois
Hi, Thanks. This is not a feature. I think this is about the const keyword and will attempt to fix it later. Romain Le 13 d?c. 2010 ? 05:30 PM, Daniel Saban?s Bov?<daniel.sabanesbove at ifspm.uzh.ch> a ?crit :
Hi,
I have just discovered an extremely strange behaviour in conversion of
an R logical to a C++ bool. Consider the minimal example:
--- snip ---
library(Rcpp)
library(inline)
## this compiles and loads fine, but is wrong:
wrong <- cxxfunction(signature(x="list") , '
List input(x);
const bool a = input[0];
return wrap(a);
' , plugin = "Rcpp")
wrong(list(TRUE))
wrong(list(FALSE))
## this is correct:
correct <- cxxfunction(signature(x="list") , '
List input(x);
const bool a = as<bool>(input[0]);
return wrap(a);
' , plugin = "Rcpp")
correct(list(TRUE))
correct(list(FALSE))
--- snap ----
In Rcpp 0.8.9 wrong(list(FALSE)) == TRUE, which is quite surprising to
the user...
Sorry if this already a known feature, but then I would be curious if
this behaviour is really wished?
Thanks in advance,
best regards
Daniel
_______________________________________________ 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
This is fixed now (rev 2789).
For the curious, it seems that the generic_proxy template made the wrong
choice between these conversion operators:
operator SEXP() const {
return get() ;
}
template <typename U> operator U() const {
return ::Rcpp::as<U>(get()) ;
}
I resolved this by forcing the conversion to bool with this:
operator bool() const {
return ::Rcpp::as<bool>(get()) ;
}
Thanks again for the report.
Romain
Le 13/12/10 17:30, Daniel Saban?s Bov? a ?crit :
Hi,
I have just discovered an extremely strange behaviour in conversion of
an R logical to a C++ bool. Consider the minimal example:
--- snip ---
library(Rcpp)
library(inline)
## this compiles and loads fine, but is wrong:
wrong<- cxxfunction(signature(x="list") , '
List input(x);
const bool a = input[0];
return wrap(a);
' , plugin = "Rcpp")
wrong(list(TRUE))
wrong(list(FALSE))
## this is correct:
correct<- cxxfunction(signature(x="list") , '
List input(x);
const bool a = as<bool>(input[0]);
return wrap(a);
' , plugin = "Rcpp")
correct(list(TRUE))
correct(list(FALSE))
--- snap ----
In Rcpp 0.8.9 wrong(list(FALSE)) == TRUE, which is quite surprising to
the user...
Sorry if this already a known feature, but then I would be curious if
this behaviour is really wished?
Thanks in advance,
best regards
Daniel
Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://bit.ly/fT2rZM : highlight 0.2-5 |- http://bit.ly/gpCSpH : Evolution of Rcpp code size `- http://bit.ly/hovakS : RcppGSL initial release