Skip to content

Commit a8c183d

Browse files
committed
Do not throw INT_MAX error when returning vector
1 parent f82ac81 commit a8c183d

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

inst/include/RcppEigenWrap.h

+9-6
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,21 @@ namespace Rcpp{
8181
// for plain dense objects
8282
template <typename T>
8383
SEXP eigen_wrap_plain_dense( const T& obj, Rcpp::traits::true_type ) {
84+
bool needs_dim = T::ColsAtCompileTime != 1;
85+
R_xlen_t m = obj.rows(), n = obj.cols();
86+
if (needs_dim && (m > INT_MAX || n > INT_MAX)) {
87+
Rcpp::stop("array dimensions cannot exceed INT_MAX");
88+
}
89+
R_xlen_t size = m * n;
8490
typename Eigen::internal::conditional<
8591
T::IsRowMajor,
8692
Eigen::Matrix<typename T::Scalar,
8793
T::RowsAtCompileTime,
8894
T::ColsAtCompileTime>,
8995
const T&>::type objCopy(obj);
90-
R_xlen_t m = obj.rows(), n = obj.cols(), size = m * n;
91-
if (m > INT_MAX || n > INT_MAX) {
92-
Rcpp::stop("array dimensions cannot exceed INT_MAX");
93-
}
94-
SEXP ans = PROTECT(::Rcpp::wrap(objCopy.data(), objCopy.data() + size));
95-
if ( T::ColsAtCompileTime != 1 ) {
96+
SEXP ans = PROTECT(::Rcpp::wrap(objCopy.data(),
97+
objCopy.data() + size));
98+
if (needs_dim) {
9699
SEXP dd = PROTECT(::Rf_allocVector(INTSXP, 2));
97100
int *d = INTEGER(dd);
98101
d[0] = m;

0 commit comments

Comments
 (0)