Le 21/10/2013 11:59, Florian Oswald a écrit :
Dear all,

I've noted that I cannot return an Rcpp::List longer than 20 elements.
In particular, this here compiles:

library(Rcpp)
library(inline)

src <- '
List out = List::create(_["x1"] = 1,
_["x2"] = 2,
_["x3"] = 3,
_["x4"] = 4,
_["x5"] = 5,
_["x6"] = 6,
_["x7"] = 7,
_["x8"] = 8,
_["x9"] = 9,
_["x10"] = 10,
_["x11"] = 11,
_["x12"] = 12,
_["x13"] = 13,
_["x14"] = 14,
_["x15"] = 15,
_["x16"] = 16,
_["x17"] = 17,
_["x18"] = 18,
_["x19"] = 19,
//_["x20"] = 20,
_["x21"] = 21);

return out;'

f <- cxxfunction(body=src,plugin='Rcpp')
f()

whereas it does not if you uncomment element "x20" with the error message

no matching function for call to
‘Rcpp::Vector<19>::create(Rcpp::traits::named_object<int>, ...

from which I deduce that the Rcpp::List is an Rcpp::Vector of length 20.
Was there any strong reason to stop at 20 elements?

Best,
Florian

create is implemented as a set of overloads that are generated by code bloating. see this: https://r-forge.r-project.org/scm/viewvc.php/pkg/Rcpp/inst/include/Rcpp/generated/Vector__create.h?view=markup&revision=4207&root=rcpp

It would be trivial to go beyond 20, but that would mean that Rcpp would have to carry additional bloat.

Instead, you can do something like the code in this gist. https://gist.github.com/romainfrancois/7117540

List out( 30 ) ;
CharacterVector names(30) ;

set_item( out, 0 , _["x0"]  = 42, names) ;
...
set_item( out, 25 , _["x24"]  = 42, names) ;


If this syntax is too much to stomach, you can smart up and write some sort of proxy class like in: https://gist.github.com/romainfrancois/7117691

In this example, you write:

    List out( 30 ) ; CharacterVector names(30) ;
    SequentialInserter inserter( out, names );

    inserter["x0"]  = 42  ;
    ...
    inserter["x29"] = 32 ;

You can probably encapsulate more so that you don't have to create the 3 objects (out, names and inserter), but I leave this as an exercize.

This is not as nice as using create, but it gets the job done in a way that does not compromise the structure you wanted to use in the first place, as was suggested otherwise in this thread.

FYI, in Rcpp11, create has been reimplemented to take advantage of variadic templates (from C++11), so this limitation disappears.

Romain

--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to