Title: Support for Exporting Template and Namespaced Functions in Rcpp · Issue #1368 · RcppCore/Rcpp · GitHub
Open Graph Title: Support for Exporting Template and Namespaced Functions in Rcpp · Issue #1368 · RcppCore/Rcpp
X Title: Support for Exporting Template and Namespaced Functions in Rcpp · Issue #1368 · RcppCore/Rcpp
Description: 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 ex...
Open Graph Description: 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 exte...
X Description: 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 exte...
Opengraph URL: https://github.com/RcppCore/Rcpp/issues/1368
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support for Exporting Template and Namespaced Functions in Rcpp","articleBody":"## Enhancement: Support for Exporting Template and Namespaced Functions in Rcpp\n\nRcpp 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.\n\n### Key Features\n\n#### 1. Support for Exporting Template Functions\n- The `Rcpp::export` attribute can now be applied to **explicit template instantiations**.\n- The function must have an explicit instantiation in the `.cpp` file for it to be correctly recognized.\n- Works seamlessly with **`Rcpp::interfaces(cpp)`**.\n\n#### 2. Support for Exporting Functions Inside Namespaces\n- Functions within namespaces can now be exported, as long as a **forward declaration** is provided with `cpp_name` in `Rcpp::export`.\n- This ensures proper symbol resolution while allowing structured code organization.\n\n#### 3. Handling of Declaration Specifiers\n- The system now accounts for **one and only one declaration specifier**:\n - `template` → Used for **explicit template instantiations**.\n - `inline` → Supports **functions defined inside headers**.\n- Combinations like `\"static inline\"` are **not supported** (e.g., `static` is explicitly disallowed).\n- An **inline function can still be forward-declared without the `inline` keyword**, and it will work seamlessly.\n\n#### 4. New `includes` Attribute for Header File Support\n- Since **Rcpp does not scan for header files**, a new `Rcpp::includes` attribute has been introduced.\n- This allows specifying one or more header file paths, ensuring necessary definitions are available in the generated `.cpp` file.\n- This is especially important for **template functions**, as the compiler needs access to their full definitions in order to instantiate them correctly.\n\n### Example Usage\n\n```cpp\n// [[Rcpp::includes(\"helpers.h\")]]\n\n// [[Rcpp::export(name = \"A1\", cpp_name = \"A1\")]]\ntemplate NumericVector A\u003cNumericVector\u003e(size_t n);\n\n// [[Rcpp::export(name = \"A2\", cpp_name = \"A2\")]]\ntemplate NumericVector A\u003cNumericVector\u003e(NumericVector x);\n\n// [[Rcpp::export(name = \"A3\", cpp_name = \"A3\")]]\nNumericVector Test::G();\n```\n\n### Generated Rcpp Export File\n\n```cpp\n// A\u003cNumericVector\u003e\ntemplate NumericVector A\u003cNumericVector\u003e(size_t n);\nRcppExport SEXP _testRcpp_A1(SEXP nSEXP) {\nBEGIN_RCPP\n Rcpp::RObject rcpp_result_gen;\n Rcpp::RNGScope rcpp_rngScope_gen;\n Rcpp::traits::input_parameter\u003c size_t \u003e::type n(nSEXP);\n rcpp_result_gen = Rcpp::wrap(A\u003cNumericVector\u003e(n));\n return rcpp_result_gen;\nEND_RCPP\n}\n\n// A\u003cNumericVector\u003e\ntemplate NumericVector A\u003cNumericVector\u003e(NumericVector x);\nRcppExport SEXP _testRcpp_A2(SEXP xSEXP) {\nBEGIN_RCPP\n Rcpp::RObject rcpp_result_gen;\n Rcpp::RNGScope rcpp_rngScope_gen;\n Rcpp::traits::input_parameter\u003c NumericVector \u003e::type x(xSEXP);\n rcpp_result_gen = Rcpp::wrap(A\u003cNumericVector\u003e(x));\n return rcpp_result_gen;\nEND_RCPP\n}\n\n// Test::G\nNumericVector Test::G();\nRcppExport SEXP _testRcpp_A3() {\nBEGIN_RCPP\n Rcpp::RObject rcpp_result_gen;\n Rcpp::RNGScope rcpp_rngScope_gen;\n rcpp_result_gen = Rcpp::wrap(Test::G());\n return rcpp_result_gen;\nEND_RCPP\n}\n\nstatic const R_CallMethodDef CallEntries[] = {\n {\"_testRcpp_A1\", (DL_FUNC) \u0026_testRcpp_A1, 1},\n {\"_testRcpp_A2\", (DL_FUNC) \u0026_testRcpp_A2, 1},\n {\"_testRcpp_A3\", (DL_FUNC) \u0026_testRcpp_A3, 0},\n {NULL, NULL, 0}\n};\n```\n\n### Testing \u0026 Future Considerations\nI 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.\n\nWould this be a feature that the maintainers would be interested in? If so, I would be happy to prepare a pull request.\n\n","author":{"url":"https://github.com/ManosPapadakis95","@type":"Person","name":"ManosPapadakis95"},"datePublished":"2025-03-21T17:40:30.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":49},"url":"https://github.com/1368/Rcpp/issues/1368"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c508d72a-5f3b-c69e-436b-45e045c0d15b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DCF0:65405:5594D2:720757:69715E26 |
| html-safe-nonce | db209d207be140011cb1ac6c6cf14e0f94ea12d3ba92084b6991190c0f8942fb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQ0YwOjY1NDA1OjU1OTREMjo3MjA3NTc6Njk3MTVFMjYiLCJ2aXNpdG9yX2lkIjoiNTU1MzA2NTE2ODQwNjE0MjUwMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9c8b94f6227ea7577eb0618c2c21fc78004a2321dd23bda0a3c0b9cf3bbd277d |
| hovercard-subject-tag | issue:2939072367 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/RcppCore/Rcpp/1368/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5b4efbe1e2f3e1b8c91872a11d908cc115229ac2cd8ca4ade9e2e2eaa1248fd6/RcppCore/Rcpp/issues/1368 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5b4efbe1e2f3e1b8c91872a11d908cc115229ac2cd8ca4ade9e2e2eaa1248fd6/RcppCore/Rcpp/issues/1368 |
| og:image:alt | 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 exte... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ManosPapadakis95 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 1c338feb2465fb28789c852d4d9bbb3a30c0620671d1df7914edfbde84531d5e |
| turbo-cache-control | no-preview |
| go-import | github.com/RcppCore/Rcpp git https://github.com/RcppCore/Rcpp.git |
| octolytics-dimension-user_id | 5811336 |
| octolytics-dimension-user_login | RcppCore |
| octolytics-dimension-repository_id | 14005591 |
| octolytics-dimension-repository_nwo | RcppCore/Rcpp |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14005591 |
| octolytics-dimension-repository_network_root_nwo | RcppCore/Rcpp |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ca28321bb5dd58db88720c48080666bfbe28520a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width