Named, arbitrary size tuple in C++

340 Views Asked by At

I am creating a library to draw samples from a bayesian model, as a backend for an R package. Thing is, MCMC algorithms tend to give a difficult time debugging. Moreover, Rcpp does not have an easy way to debug; in practice I ended up with a massive amount of "cout" statements.

The issue is when I tried to move everything to a standalone library, I realized I fell in love with Rcpp's List. Is a really neat way to store samples with different dimensions. I tried to understand Rcpp's implementation but sincerely I could not comprehend it (which is based on policies) to try to replicate it.

The question: Is there any way to implement an arbitrary sized, named tuple? (in a broad sense, not necessary using C++ tuples).

I know I could link Rcpp using the R install path, but I'm not sure if this could be a good practice or if I will have problems trying to upload the R package to CRAN (they are very strict) or using it as a standalone library for users not having R.

Thanks!

1

There are 1 best solutions below

0
On

I am not sure this is what you want. If you want to build a list with arbitrary size in Rcpp and return to R side, you can try something like below:

std::vector<std::string> names;

std::vector<SEXP> elements;

// do something with the elements and names

Rcpp::List result(elements.size());

for (size_t i = 0; i < elements.size(); ++i) {
    result[i] = elements[i];
}

result.attr("names") = Rcpp::wrap(names);
// result can be return to R as a list