Skip to content

Cannot sapply lambda functions #213

@romainfrancois

Description

@romainfrancois

With the cpp11 plugin on, Rcpp cannot sapply lambda functions.

#include <Rcpp.h>
using namespace Rcpp ;

// [[Rcpp:::plugins(cpp11)]]

// [[Rcpp::export]]
NumericVector double_up( NumericVector x ){
  return sapply( x, [](double x){ return x*x; } )
}

/*** R
  double_up( c(1,2) )
**/

I get :

$ RcppScript /tmp/double.cpp
In file included from double.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include/Rcpp.h:27:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include/RcppCommon.h:159:
In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include/Rcpp/traits/traits.h:74:
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include/Rcpp/traits/result_of.h:31:22: error: no type named 'result_type' in
      '<lambda at double.cpp:8:21>'
        typedef typename T::result_type type ;
                ~~~~~~~~~~~~^~~~~~~~~~~
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include/Rcpp/sugar/functions/sapply.h:105:34: note: in instantiation of template class
      'Rcpp::traits::result_of<<lambda at double.cpp:8:21> >' requested here
        typename ::Rcpp::traits::result_of<Function>::type ,
                                 ^
double.cpp:8:10: note: while substituting deduced template arguments into function template 'sapply' [with RTYPE = 14, NA = true, T = Rcpp::Vector<14, PreserveStorage>,
      Function = <lambda at double.cpp:8:21>]
  return sapply( x, [](double x){ return x*x; } )
         ^
double.cpp:8:10: error: no matching function for call to 'sapply'
  return sapply( x, [](double x){ return x*x; } )
         ^~~~~~
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include/Rcpp/vector/ListOf.h:134:3: note: candidate template ignored: could not match 'ListOf' against
      'Vector'
T sapply(const ListOf<T>& t, Function fun) {
  ^
/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include/Rcpp/sugar/functions/sapply.h:109:1: note: candidate template ignored: substitution failure
      [with RTYPE = 14, NA = true, T = Rcpp::Vector<14, PreserveStorage>, Function = <lambda at double.cpp:8:21>]
sapply( const Rcpp::VectorBase<RTYPE,NA,T>& t, Function fun ){
^
2 errors generated.
make: *** [double.o] Error 1
clang++ -std=c++11 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include  -I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include"    -fPIC  -O3 -c double.cpp -o double.o
Erreur dans sourceCpp(tail(args, 1), verbose = "-v" %in% args) :
  Error 1 occurred building shared library.
Exécution arrêtée

This is mainly Rcpp::traits:: result_of's fault.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions