{"id":15048004,"url":"https://github.com/jonclayden/rcpparray","last_synced_at":"2025-04-10T01:11:19.727Z","repository":{"id":65535417,"uuid":"581117056","full_name":"jonclayden/RcppArray","owner":"jonclayden","description":"Simple compatibility layer between Rcpp and std::array / std::span","archived":false,"fork":false,"pushed_at":"2025-01-08T16:31:40.000Z","size":78,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-10T01:11:13.120Z","etag":null,"topics":["array","cpp11","cpp20","r","rcpp","span"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonclayden.png","metadata":{"files":{"readme":"README.md","changelog":"NEWS.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-22T10:14:17.000Z","updated_at":"2025-01-08T16:31:45.000Z","dependencies_parsed_at":"2024-06-02T14:30:58.008Z","dependency_job_id":"c8faa7d7-0c6a-4f54-823a-8727f514e629","html_url":"https://github.com/jonclayden/RcppArray","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonclayden%2FRcppArray","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonclayden%2FRcppArray/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonclayden%2FRcppArray/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonclayden%2FRcppArray/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonclayden","download_url":"https://codeload.github.com/jonclayden/RcppArray/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248137886,"owners_count":21053775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","cpp11","cpp20","r","rcpp","span"],"created_at":"2024-09-24T21:06:46.836Z","updated_at":"2025-04-10T01:11:19.713Z","avatar_url":"https://github.com/jonclayden.png","language":"C++","readme":"[![CRAN version](https://www.r-pkg.org/badges/version/RcppArray)](https://cran.r-project.org/package=RcppArray) [![CI](https://github.com/jonclayden/RcppArray/actions/workflows/ci.yaml/badge.svg)](https://github.com/jonclayden/RcppArray/actions/workflows/ci.yaml) [![Dependencies](https://tinyverse.netlify.app/badge/RcppArray)](https://tinyverse.netlify.app)\n\n\n# `Rcpp` interoperability with `std::array` (et al.)\n\n[`Rcpp`](https://www.rcpp.org) is a mature and very widely used package providing seamless interoperability between C++ and [the R language](https://www.r-project.org), which has a native C API. This package facilitates conversion between R data structures and the templated [`std::array` class](https://en.cppreference.com/w/cpp/container/array) introduced in C++11, by using the interface provided in `Rcpp`. It originated with [this Stack Overflow question](https://stackoverflow.com/questions/74887786/specialising-rcppas-for-stdarray).\n\nSupport for [`std::tuple`](https://en.cppreference.com/w/cpp/utility/tuple) was added in package version 0.3.0. This data structure, also introduced in C++11, combines a fixed set of elements of potentially different types.\n\n`std::array` is a templated container type with a fixed number of elements, an object-orientated analogue of a C-style array type like `int[3]`. Client packages can interface this type with R if they add `RcppArray` to `LinkingTo` and include the header. (A simple [example package](https://github.com/jonclayden/RcppArray/tree/main/clients/array.test) is provided.)\n\n```c++\n// No need to include \"Rcpp.h\" as well\n#include \"RcppArray.h\"\n\n// [[Rcpp::export]]\nRObject test() {\n  Rcpp::NumericVector vec = Rcpp::NumericVector::create(1,2,3);\n  std::array\u003cdouble,3\u003e arr = Rcpp::as\u003cstd::array\u003cdouble,3\u003e\u003e(vec);\n  // Do something with the array\n  \n  std::array\u003cunsigned int,3\u003e result = { 1, 2, 3 };\n  return result;    // Implicitly Rcpp::wrap(result)\n}\n```\n\nIn either direction the element type of the array can be any atomic type that `Rcpp` knows how to convert, and isn't limited to the types that R uses internally. In the example above, `unsigned int` is used even though it doesn't directly correspond to an R vector mode: the `wrap()` function will convert it to an R `numeric` vector, i.e., `double`.\n\nA tuple, declared in C++ via a variadic template, as in `std::tuple\u003cint, double, std::string\u003e`, can be converted similarly using `Rcpp::as()` and `Rcpp::wrap()`. In this case the R analogue is a list rather than an atomic vector, since the element types can vary.\n\n## std::span\n\nThere is also experimental support for [`std::span`](https://en.cppreference.com/w/cpp/container/span), introduced in C++20. This is a typed container of fixed or dynamic length which provides a \"view\" onto a contiguous block of data owned by another object. Once again, there is a simple [example client package](https://github.com/jonclayden/RcppArray/tree/main/clients/span.test) showing usage of this facility.\n\nIn this case there are some notable caveats. Client packages must obviously request C++20 support, and [a `configure` script](https://github.com/jonclayden/RcppArray/blob/main/clients/span.test/configure.ac) may well be needed to check for `span` availability. Moreover, since a `span` does not own the memory it points to, `as\u003cspan\u003cT,D\u003e\u003e()` will only compile where the requested type `T` matches a type that R uses internally (viz. `int`, `double`, etc.). The latter limitation does not apply to `wrap`, however, because a new vector is created and the data converted.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonclayden%2Frcpparray","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonclayden%2Frcpparray","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonclayden%2Frcpparray/lists"}