[Rcpp-devel] Underflow of unsigned integers

Xavier Robin xavier at cbs.dtu.dk
Wed Mar 5 12:07:06 CET 2014


Given the following function:
> // [[Rcpp::export]]
> void test_size_t(size_t s) {
>     std::cout << s << std::endl;
> }
compiled with sourceCpp or in a package, I get the following behavior:
> > test_size_t(-1)
> 18446744073709551615
Of course this is not what I want. I would be OK with the value being 
set to 0, or preferably an error being thrown for instance...

Therefore I tried to defined a custom as<size_t>() as explained in 
[http://gallery.rcpp.org/articles/custom-as-and-wrap-example/]. In a 
convert.h file I defined:
> #include <RcppCommon.h>
> namespace Rcpp {
>     // size_t
>     template <> size_t as(SEXP ptr);
>     ...
> }
And in the corresponding convert.cpp:
> #include "boost/numeric/conversion/cast.hpp"
> #include <Rcpp.h>
> namespace Rcpp {
>     // size_t
>     template <> size_t as(SEXP ptr) {
>         long long i(as<long long>(ptr));
>         return boost::numeric_cast<size_t>(i);
>     }
>     ...
> }
Unfortunately my custom as<size_t>() is apparently not executed. I still 
get overflows despite the fact I included the convert.h in the cpp file 
I source, and when printing some output in as() nothing is printed out.

Of course I could define my function as void test_size_t(int i) and do 
the conversion there, but I'd prefer some more fool-proof solution.

Any ideas?

Best,
Xavier

-- 
Xavier Robin, PhD
Cellular Signal Integration Group (C-SIG) - http://www.lindinglab.org
Center for Biological Sequence Analysis (CBS) - http://www.cbs.dtu.dk
Department of Systems Biology - Technical University of Denmark (DTU)
Anker Engelundsvej, Building 301, DK-2800 Lyngby, DENMARK.



More information about the Rcpp-devel mailing list