【发布时间】:2014-10-06 13:18:49
【问题描述】:
我正在尝试使用 Rcpp 为 C++ 脚本编写 R 绑定。其中一个函数需要std::shared_ptr object。我发现很难初始化 std::shared_ptr obj 并将其作为 Rcpp::XPtr 对象返回到 R 端。
我试过了(最小的例子):
#include <iostream>
#include <memory>
#include <Rcpp.h>
using namespace Rcpp;
using std::cout;
class TestClass {
public:
int value;
TestClass(int initial_val) {
value = initial_val;
};
};
//[[Rcpp::export]]
SEXP get_test_obj() {
Rcpp::XPtr<std::shared_ptr<TestClass>> ptr(std::make_shared<TestClass>(5), true);
return ptr;
};
但是得到以下错误:
no matching function for call to 'Rcpp::XPtr<std::shared_ptr<TestClass> >::XPtr(std::shared_ptr<TestClass>, bool)'
关于如何做到这一点的任何想法?还是我做错了?
【问题讨论】:
-
有几个项目使用
Rcpp::XPtr,也许更仔细地看看他们所做的可以帮助你吗?