以 NumericVector& 作为参数的构造函数的 Rcpp 模块错误

Rcpp module error with a constructor taking NumericVector& as parameter

我的构造函数有问题,看起来像 MyModel::MyModel(const Rcpp::NumericVector& ModelData) 在下面的代码中。

它在我的 新 mac 版本(带有 R 3.6.0 的 clang7)上给出了一个新错误 [=27= 没有错误],并且 在 mac 上使用旧版 (<=3.0.?) 没有错误。

我想我做错了什么,之前只发出警告,现在是错误。任何人都可以帮忙吗? (我将其发布在 Rcpp 邮件列表中) 下面是代码和错误。

code='
#include <Rcpp.h>
using namespace Rcpp;
class MyModel{  public: MyModel(const Rcpp::NumericVector& ModelData) {};};
RCPP_MODULE(MyModel){
    Rcpp::class_<MyModel>(\"MyModel\")
    .constructor<const Rcpp::NumericVector &>()
    ;
}'

sourceCpp(code=code)

In file included from file5a40416569b1.cpp:2:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/Rcpp.h:27:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/RcppCommon.h:168:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/Rcpp/as.h:25:
/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/Rcpp/internal/Exporter.h:31:30: error: reference member 't' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object
Exporter( SEXP x ) : t(x){} ^
/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/Rcpp/as.h:87:41: note: in instantiation of member function 'Rcpp::traits::Exporter &>::Exporter' requested here
::Rcpp::traits::Exporter exporter(x); ^
/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/Rcpp/as.h:152:26: note: in instantiation of function template specialization 'Rcpp::internal::as &>' requested here
return internal::as(x, typename traits::r_type_traits::r_category()); ^
/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/Rcpp/module/Module_generated_Constructor.h:47:27: note: in instantiation of function template specialization 'Rcpp::as &>' requested here
return new Class( as(args[0]) ) ; ^
/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/Rcpp/module/Module_generated_class_constructor.h:99:29: note: in instantiation of member function 'Rcpp::Constructor_1 &>::get_new' requested here
AddConstructor( new Constructor_1 , valid, docstring ) ; ^
file5a40416569b1.cpp:7:6: note: in instantiation of function template specialization 'Rcpp::class_::constructor &>' requested here
.constructor() ^
/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/Rcpp/internal/Exporter.h:35:9: note: reference member declared here T t ; ^
1 error generated. make: *** [file5a40416569b1.o] Error 1
/usr/local/clang7/bin/clang++ -Wall -
I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -
I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include" -
I"/private/var/folders/zt/7cz1y9md79l_h08bbqymt4w9z8xlw7/T/RtmpZZ6sit/sourceCpp-x86_64-apple-darwin15.6.0-1.0.1" -isysroot
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -
I/usr/local/include -fPIC -Wall -g -O2 -c file5a40416569b1.cpp -o file5a40416569b1.o
Error in sourceCpp(code = code) :
Error 1 occurred building shared library.

我们刚刚在 follow-up 讨论 rcpp-devel 列表时修复了 minimally complete and verifiable example 出现的问题 - 最初在这里不提供一个会浪费每个人的时间。

简而言之,我们无法使用指向 S 表达式对象的 SEXP 指针对引用语义进行 &。只需删除 & 即可修复它。更正了下面的代码。

#include <Rcpp.h>
using namespace Rcpp;

class MyModel {
public:
  double a;
  ~MyModel();
  MyModel();
  MyModel(const Rcpp::NumericVector ModelData, int temp);
};

RCPP_EXPOSED_CLASS(MyModel)

MyModel::~MyModel() {}
MyModel::MyModel() : a(0) {}
MyModel::MyModel(const Rcpp::NumericVector ModelData, int temp) : a(0) {}

RCPP_MODULE(MyModel) {
  Rcpp::class_<MyModel>("MyModel")
  .constructor()
  .constructor<const Rcpp::NumericVector, int>()
  ;
}