Skip to content

Commit 30ff4a1

Browse files
authored
Merge pull request #908 from romainfrancois/bug/Matrix-no-init
Matrix(no_init(,)) sets the nrows member
2 parents 4680c31 + 6fedfc2 commit 30ff4a1

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

inst/include/Rcpp/vector/Matrix.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ class Matrix : public Vector<RTYPE, StoragePolicy>, public MatrixBase<RTYPE, tru
8989
}
9090
Matrix& operator=( const SubMatrix<RTYPE>& ) ;
9191

92-
explicit Matrix( const no_init_matrix& obj) {
93-
VECTOR::set__( Rf_allocMatrix( RTYPE, obj.nrow(), obj.ncol() ) );
94-
}
92+
explicit Matrix( const no_init_matrix& obj) : VECTOR(Rf_allocMatrix(RTYPE, obj.nrow(), obj.ncol())), nrows(obj.nrow()) {}
9593

9694
inline int ncol() const {
9795
return VECTOR::dims()[1];

inst/unitTests/cpp/Matrix.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ NumericMatrix runit_no_init_matrix_ctor() {
288288
return x;
289289
}
290290

291+
// [[Rcpp::export]]
292+
int runit_no_init_matrix_ctor_nrow() {
293+
NumericMatrix x(no_init(2, 2));
294+
return x.nrow();
295+
}
296+
291297
void runit_const_Matrix_column_set( NumericMatrix::Column& col1, const NumericMatrix::Column& col2 ){
292298
col1 = col2 ;
293299
}

inst/unitTests/runit.Matrix.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ if (.runThisTest) {
202202
checkEquals(m, matrix(c(0, 1, 2, 3), nrow = 2))
203203
}
204204

205+
test.NumericMatrix.no.init.ctor.nrow <- function() {
206+
nrow <- runit_no_init_matrix_ctor_nrow()
207+
checkEquals(nrow, 2L)
208+
}
209+
205210
test.NumericMatrix.const.Column <- function(){
206211
m <- matrix(as.numeric(1:9), nrow = 3)
207212
res <- runit_const_Matrix_column(m)

0 commit comments

Comments
 (0)