Skip to content

Commit 7664ed5

Browse files
fix const correctness of Matrix::Column( const Matrix&, int)
1 parent 67cfd72 commit 7664ed5

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

inst/include/Rcpp/vector/MatrixColumn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class MatrixColumn : public VectorBase<RTYPE,true,MatrixColumn<RTYPE> > {
4444

4545
MatrixColumn( const MATRIX& parent, int i ) :
4646
n(parent.nrow()),
47-
start(parent.begin() + i * n ),
47+
start( const_cast<MATRIX&>(parent).begin() + i * n ),
4848
const_start(parent.begin() + i *n)
4949
{
5050
if( i < 0 || i >= parent.ncol() ) throw index_out_of_bounds() ;

inst/unitTests/cpp/Matrix.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,16 @@ NumericMatrix runit_no_init_matrix() {
225225
}
226226
return x;
227227
}
228+
229+
void runit_const_Matrix_column_set( NumericMatrix::Column& col1, const NumericMatrix::Column& col2 ){
230+
col1 = col2 ;
231+
}
232+
233+
// [[Rcpp::export]]
234+
NumericVector runit_const_Matrix_column( const NumericMatrix& m ){
235+
NumericMatrix::Column col1( m, 0 ) ;
236+
NumericMatrix::Column col2( m, 1 ) ;
237+
runit_const_Matrix_column_set(col1, col2) ;
238+
return col1 ;
239+
}
240+

inst/unitTests/runit.Matrix.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,10 @@ if (.runThisTest) {
165165
checkEquals(m, matrix(c(0, 1, 2, 3), nrow = 2))
166166
}
167167

168+
test.NumericMatrix.const.Column <- function(){
169+
m <- matrix(as.numeric(1:9), nrow = 3)
170+
res <- runit_const_Matrix_column(m)
171+
checkEquals( m[,1], m[,2] )
172+
}
168173

169174
}

0 commit comments

Comments
 (0)