gpt4 book ai didi

c++ - 使用 Rcpp 公开 C++ 类

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:47 27 4
gpt4 key购买 nike

我一直在研究 Rcpp,目前出现了几个问题......

根据我的理解,如果你想向 R 公开一个 C++ 类,你需要为 Rcpp::wrap 和 Rcpp::as 编写部分模板特化。我查看了这是如何在 Rcpp::Date 类中完成的,我有以下问题:- 在 Date.h 中我们有:

// template specialisation for wrap() on the date
// OK as explained in docs for non intrusive
// partial template specialization
template <> SEXP wrap<Rcpp::Date>(const Rcpp::Date &date);

在标题的下方,您有以下代码:

template<> inline SEXP wrap_extra_steps<Rcpp::Date>( SEXP x ){
Rf_setAttrib( x, R_ClassSymbol, Rf_mkString( "Date" ) ) ;
return x ;
}

wrap_extra_steps 应该做什么?是必需的吗?同样在Date.cpp中wrap方法实现如下:

template <> SEXP wrap(const Date &date) {
return internal::new_date_object( date.getDate() ) ;
}

将 internal::new_date_object 实现为:

SEXP new_date_object( double d){
SEXP x = PROTECT(Rf_ScalarReal( d ) ) ;
Rf_setAttrib(x, R_ClassSymbol, Rf_mkString("Date"));
UNPROTECT(1);
return x;
}

好吧,我知道一个 SEXP 被创建并返回给 R,但是我没有得到 PROTECT()、Rf_setAttrib、UNPROTECT 的整个部分...这里发生了什么?

谢谢!

最佳答案

有一个完整的小插图讨论如何写 as<>()wrap() ---Rcpp 扩展小插图。

正如它所讨论的,部分特化只是三种方法之一,还有其他示例包。 Date()是 Rcpp 本身实现的东西,所以它不是最好的例子。阅读小插图,研究其他示例并在 rcpp-devel 上提问。

关于c++ - 使用 Rcpp 公开 C++ 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16496577/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com