Skip to content

Commit e6a267b

Browse files
committed
index lapply operands through VectorBase so vectors yield their stored type
Holding the operand as the derived vector type made lapply() pass a proxy (e.g. const_string_proxy for character vectors) to the function, which no longer converted to a const CharacterVector& argument. Seen as a compile failure in the touch package.
1 parent d4cbe58 commit e6a267b

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎inst/include/Rcpp/sugar/functions/lapply.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ class Lapply : public VectorBase<
4040
vec(vec_.get_ref()), fun(fun_){}
4141

4242
inline SEXP operator[]( R_xlen_t i ) const {
43-
return Rcpp::wrap( fun( vec[i] ) );
43+
// index through VectorBase so that vectors yield their stored
44+
// type (e.g. SEXP for character vectors) rather than a proxy
45+
return Rcpp::wrap( fun( static_cast<const VEC&>(vec)[i] ) );
4446
}
4547
inline R_xlen_t size() const { return vec.size() ; }
4648

‎inst/tinytest/cpp/sugar.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,18 @@ List runit_lapply_seq( IntegerVector xx){
245245
return res ;
246246
}
247247

248+
CharacterVector lapply_string_rev( const CharacterVector& x ){
249+
std::string s = as<std::string>( x[0] ) ;
250+
return CharacterVector::create( std::string( s.rbegin(), s.rend() ) ) ;
251+
}
252+
253+
// the element passed to the function must be the stored SEXP, not a proxy
254+
// [[Rcpp::export]]
255+
List runit_lapply_string( CharacterVector xx ){
256+
List res = lapply( xx, lapply_string_rev ) ;
257+
return res ;
258+
}
259+
248260
// [[Rcpp::export]]
249261
NumericVector runit_mapply2(NumericVector xx, NumericVector yy){
250262
NumericVector res = mapply(xx, yy, std::plus<double>());

‎inst/tinytest/test_sugar.R‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ expect_equal( fx( 1:10 ), as.list((1:10)^2) )
341341
fx <- runit_lapply_seq
342342
expect_equal( fx( 1:10 ), lapply( 1:10, seq_len ) )
343343

344+
fx <- runit_lapply_string
345+
expect_equal( fx( c("abc", "de") ), list("cba", "ed") )
346+
344347

345348
# test.sugar.mapply2 <- function( ){
346349
fx <- runit_mapply2

0 commit comments

Comments
 (0)