[Rcpp-devel] bool from list: unexpected result and differences between compilers/OSs

Ramon Diaz-Uriarte rdiaz02 at gmail.com
Mon Jun 22 10:25:28 CEST 2015


Dear All,

Accessing an element of a list that should be TRUE/FALSE does not recognize
the boolean properly in Windows/gcc-4.6.3 unless I use "as". It does the
same surprising thing in Debian Linux with gcc-4.6.4.

But it does do what I expect in Linux, with both gcc (4.9.2) and
clang++-libc++ (3.5.2-1). In Windows, I am using Rtools33.exe, the latest
Rtools.exe (downloaded last night). 



Given the above, is it the proper way to proceed to always use "as<bool>"?
Or, just being extra careful, should we "as<whatever>" (double, int, etc)?


Best,

R.




/////////////////////////////////////////////////////
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
bool readBool(bool boolR) {
    bool boolC = boolR;
    Rcpp::Rcout << "boolC " << boolC << std::endl;
    return boolC;
}

// Does not do what I expect in windows

// [[Rcpp::export]]
bool readList(Rcpp::List theList) {
    bool boolC = theList["theBool"];
    Rcpp::Rcout << "boolC " << boolC << std::endl;
    return boolC;
}

// The following works OK in the three systems

// [[Rcpp::export]]
bool readList2(Rcpp::List theList) {
    bool boolC = as<bool>(theList["theBool"]);
    Rcpp::Rcout << "boolC " << boolC << std::endl;
    return boolC;
}


/***R

readBool(TRUE)
readBool(FALSE)

lTrue <- list(a = 99, theBool = TRUE)
lFalse <- list(a = 99, theBool = FALSE)

readList(lTrue) == lTrue[["theBool"]]
readList(lFalse) == lFalse[["theBool"]] ## WRONG! should be 0

readList2(lTrue) == lTrue[["theBool"]]
readList2(lFalse) == lFalse[["theBool"]] ## OK now: should be 0

*/

////////////////////////////////////////////////


-- 
Ramon Diaz-Uriarte
Department of Biochemistry, Lab B-25
Facultad de Medicina
Universidad Autónoma de Madrid 
Arzobispo Morcillo, 4
28029 Madrid
Spain

Phone: +34-91-497-2412

Email: rdiaz02 at gmail.com
       ramon.diaz at iib.uam.es

http://ligarto.org/rdiaz


More information about the Rcpp-devel mailing list