Support for Exporting Template and Namespaced Functions in Rcpp

未关闭
#1,368 49 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
cpp, r
领域
tooling

调研方向

Start by reviewing the existing Rcpp::export attribute handling and the generated Rcpp export file shown in the issue. Trace how explicit template instantiations, namespaced functions, declaration specifiers, and the new Rcpp::includes attribute would be recognized; done means these examples generate valid wrappers and preserve cpp interfaces support.

由索引模型根据 Issue 内容生成。

描述

Enhancement: Support for Exporting Template and Namespaced Functions in Rcpp

Rcpp has been an incredible tool for bridging R and C++, and its flexibility has inspired me to explore new ways to extend its functionality. Building on the existing framework, I have added support for exporting template functions and functions inside namespaces, expanding the possibilities for advanced C++ integration.

Key Features
1. Support for Exporting Template Functions
  • The Rcpp::export attribute can now be applied to explicit template instantiations.
  • The function must have an explicit instantiation in the .cpp file for it to be correctly recognized.
  • Works seamlessly with Rcpp::interfaces(cpp).
2. Support for Exporting Functions Inside Namespaces
  • Functions within namespaces can now be exported, as long as a forward declaration is provided with cpp_name in Rcpp::export.
  • This ensures proper symbol resolution while allowing structured code organization.
3. Handling of Declaration Specifiers
  • The system now accounts for one and only one declaration specifier:
    • template → Used for explicit template instantiations.
    • inline → Supports functions defined inside headers.
  • Combinations like "static inline" are not supported (e.g., static is explicitly disallowed).
  • An inline function can still be forward-declared without the inline keyword, and it will work seamlessly.
4. New includes Attribute for Header File Support
  • Since Rcpp does not scan for header files, a new Rcpp::includes attribute has been introduced.
  • This allows specifying one or more header file paths, ensuring necessary definitions are available in the generated .cpp file.
  • This is especially important for template functions, as the compiler needs access to their full definitions in order to instantiate them correctly.
Example Usage
// [[Rcpp::includes("helpers.h")]]

// [[Rcpp::export(name = "A1", cpp_name = "A1")]]
template NumericVector A<NumericVector>(size_t n);

// [[Rcpp::export(name = "A2", cpp_name = "A2")]]
template NumericVector A<NumericVector>(NumericVector x);

// [[Rcpp::export(name = "A3", cpp_name = "A3")]]
NumericVector Test::G();
Generated Rcpp Export File
// A<NumericVector>
template NumericVector A<NumericVector>(size_t n);
RcppExport SEXP _testRcpp_A1(SEXP nSEXP) {
BEGIN_RCPP
    Rcpp::RObject rcpp_result_gen;
    Rcpp::RNGScope rcpp_rngScope_gen;
    Rcpp::traits::input_parameter< size_t >::type n(nSEXP);
    rcpp_result_gen = Rcpp::wrap(A<NumericVector>(n));
    return rcpp_result_gen;
END_RCPP
}

// A<NumericVector>
template NumericVector A<NumericVector>(NumericVector x);
RcppExport SEXP _testRcpp_A2(SEXP xSEXP) {
BEGIN_RCPP
    Rcpp::RObject rcpp_result_gen;
    Rcpp::RNGScope rcpp_rngScope_gen;
    Rcpp::traits::input_parameter< NumericVector >::type x(xSEXP);
    rcpp_result_gen = Rcpp::wrap(A<NumericVector>(x));
    return rcpp_result_gen;
END_RCPP
}

// Test::G
NumericVector Test::G();
RcppExport SEXP _testRcpp_A3() {
BEGIN_RCPP
    Rcpp::RObject rcpp_result_gen;
    Rcpp::RNGScope rcpp_rngScope_gen;
    rcpp_result_gen = Rcpp::wrap(Test::G());
    return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
    {"_testRcpp_A1", (DL_FUNC) &_testRcpp_A1, 1},
    {"_testRcpp_A2", (DL_FUNC) &_testRcpp_A2, 1},
    {"_testRcpp_A3", (DL_FUNC) &_testRcpp_A3, 0},
    {NULL, NULL, 0}
};
Testing & Future Considerations

I have run extensive tests to ensure this new feature works correctly. The ability to export template and namespaced functions integrates well with the existing Rcpp framework. However, given the complexity of C++ templates and namespaces, there might still be edge cases that I haven’t encountered.

Would this be a feature that the maintainers would be interested in? If so, I would be happy to prepare a pull request.

主要语言
C++
星标
799
派生
219
平均合并
5 小时 13 分钟
30 天内合并 PR
6

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

RcppCore/Rcpp 的其他 Issue

查看 RcppCore/Rcpp 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。