[Rcpp-devel] Problem with IntegerVector::value_type in recent Rcpp

Romain François romain at r-enthusiasts.com
Tue Mar 31 19:25:50 CEST 2015


Probably just my incompetence at the time of writing it. 
Would be trivial enough to fix in Rcpp for anyone with the proper skills and willingness. I only have 1.05 of those 2. 

Romain

> Le 31 mars 2015 à 17:39, Kevin Thornton <krthornt at uci.edu> a écrit :
> 
> Thanks for the explanation, Romain.
> 
> As a follow-up, is there a reason why value_type is defined in terms of a reference?  In the mean time, I'll use remove_reference.
> 
> -Kevin
> 
> 
>> On Mar 31, 2015, at 1:04 AM, Romain François <romain at r-enthusiasts.com> wrote:
>> 
>> Hi, 
>> 
>> That’s because VTYPE::value_type is traits::r_vector_proxy<RTYPE>::type, i.e. 
>> 
>> template <int RTYPE>
>> struct r_vector_proxy{
>> 	typedef typename storage_type<RTYPE>::type& type ;
>> } ;
>> 	
>> so VTYPE::value_type is int& 
>> 
>> You can either use stored_type, i.e. 
>> 
>> std::for_each( v.begin(), v.end(),[]( const VTYPE::stored_type& ) ...
>> 
>> 
>> Or do a little dance with the reference qualifier: 
>> 
>> std::for_each( v.begin(), v.end(),[]( const std::remove_reference<VTYPE::value_type&>::type& __n )  ...
>> 
>> Both of which are not very satisfying. 
>> 
>> 
>> FWIW, in Rcpp11/Rcpp14, VTYPE::value_type is defined like this: 
>> 
>> typedef typename traits::storage_type<RTYPE>::type value_type ;
>> 
>> so IntegerVector::value_type is int, and therefore you get what you rightfully expect. 
>> 
>> 
>> Romain
>> 
>> 
>>> Le 31 mars 2015 à 00:19, Kevin Thornton <krthornt at uci.edu> a écrit :
>>> 
>>> Hi,
>>> 
>>> I've come across an issue when compiling some code with Rcpp >= 0.11.4 (using R 3.1.3 with either gcc or clang on Linux, or clang on OS X).
>>> 
>>> The code that reproduces the issue is here: https://gist.github.com/molpopgen/b3bda09590172044ff84
>>> 
>>> Specifically, the last function is where I'm running into trouble.  I also tried the same ideas using C++98 (function objects instead of lambda expressions), and got the same results.
>>> 
>>> The short version is that Rcpp::IntegerVector::value_type appears to lose some info about value_type's const-ness when a const IntegerVector is passed to an Rcpp function.  I went back an tried older version of Rcpp and verified that the code from the above link compiles with version from 0.11.0 through 0.11.3.  Starting with 0.11.4, the last function in that piece of code will no longer compile, and appears to think that a non-const int & is the type that it is looking for.
>>> 
>>> I tried the other STL-like typedefs that one may expect ( reference, const_reference ), and they do not exist for Rcpp vector types.
>>> 
>>> Any thoughts?  Is this intended?  Should I not write functions taking const references to Rcpp types?
>>> 
>>> Best,
>>> 
>>> Kevin
>>> 
>>> ___________________________
>>> Kevin Thornton
>>> Associate Professor
>>> Ecology and Evolutionary Biology
>>> University of California, Irvine
>>> http://www.molpopgen.org
>>> http://github.com/molpopgen
>>> http://github.com/ThorntonLab
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> _______________________________________________
>>> 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
>> 
> 
> ___________________________
> Kevin Thornton
> Associate Professor
> Ecology and Evolutionary Biology
> University of California, Irvine
> http://www.molpopgen.org
> http://github.com/molpopgen
> http://github.com/ThorntonLab
> 
> 
> 
> 
> 
> 



More information about the Rcpp-devel mailing list