lundi 2 janvier 2017

Rcpp allows C++11 template and lambda expression? Fatal error in R

Since I need to pass a function member in a class as a parameter to another function, I used the template and lambda expression in my C++11 codes, then use the module to expose the class to build a package. The build & load works smoothly. Check seems good too. In R, I can use library() to load my package, and initiate class. Everything seems OK until I call my function. The R reports a fatal error, then terminate the session.

After all the build and installation. In R, I have no issues with the first 3 lines.

> library(myRcppPackage)
> aa <- new(ABC)

But the fatal error occurred in R at, then my RStudio was terminated

aa$doSomething(3,4)

To let people better explore the problem, I made my C++ in a minimal to show the issue.

#include <Rcpp.h>
// [[Rcpp::plugins(cpp11)]]
using namespace Rcpp;
using namespace std;
class ABC{
private:
  int x =3;
  int add2num(int a, int b){
    return a+b+x;
  }

  template<class functor_t>
  int worker(const functor_t& fun, int a, int b){
    return fun(a,b);
  }

public:

  int doSomething(int a, int b) {
    return worker([this](int a, int b){ return add2num(a, b); }, a, b);
  }

};

RCPP_MODULE(my_module){
  class_<ABC>("ABC")
  .method("doSomething", &ABC::doSomething);
}

Aucun commentaire:

Enregistrer un commentaire