title: Most efficient way to return Eigen::VectorXi with more than 2^31-1 elements to R
body: <p>I have a vector <code>x</code> of type <code>Eigen::VectorXi</code> with more than 2^31-1 entries, which I would like to return to R. I can do that by copying <code>x</code> entry-wisely to a new vector of type <code>Rcpp::IntegerVector</code>, but that seems to be quite slow.</p>
  <p>I am wondering:</p>
  <ol>
  <li>whether there is a more efficient workaround;</li>
  <li>why in the following reproducible example <code>Rcpp::wrap(x)</code> doesn't work.</li>
  </ol>
  <p>test.cpp</p>
  <pre><code>#include &lt;RcppEigen.h&gt;
  
  // [[Rcpp::depends(RcppEigen)]]
  
  // [[Rcpp::export]]
  SEXP foo(const R_xlen_t size) {
    Eigen::VectorXi x(size);
    for (R_xlen_t i = 0; i &lt; size; i++) {
      x(i) = 1;
    }
    return Rcpp::wrap(x);
  }
      
  // [[Rcpp::export]]
  Rcpp::IntegerVector fooSlow(const R_xlen_t size) {
    Eigen::VectorXi x(size);
    for (R_xlen_t i = 0; i &lt; size; i++) {
      x(i) = 1;
    }
    Rcpp::IntegerVector y(size);
    for (R_xlen_t i = 0; i &lt; size; i++) {
      y(i) = x(i);
    }   
    return y;
  }
  </code></pre>
  <p>test.R</p>
  <pre><code>Rcpp::sourceCpp(&quot;./test.cpp&quot;)
  a &lt;- foo(2^(31)) # Error in foo(2^(31)) : negative length vectors are not allowed
  a &lt;- fooSlow(2^(31)) # runs fine but it's slow
  </code></pre>