Boost logo

Boost :

From: Joerg Walter (jhr.walter_at_[hidden])
Date: 2002-10-06 12:14:27


> ----- Original Message -----
> From: "Toon Knapen" <toon.knapen_at_[hidden]>
> To: <boost_at_[hidden]>
> Sent: Thursday, October 03, 2002 9:38 AM
> Subject: [boost] ublas matrix_row<>::operator[] compile error
>
>
> > Following code snippet demonstrates you can't get an element out of a
> > matrix_row (using operator[]) on a const_matrix without making the
> > matrix_row itself const (tested on gcc-3.2). Should'nt a
> > const matrix_row< const matrix< double > > be equivalent to a
> > matrix_row< const matrix< double > > ?

No, IMO, because const matrix_row< matrix< double > > would be equivalent to
a
matrix_row< matrix< double > > then.

> > #include <boost/numeric/ublas/matrix.hpp>
> > #include <boost/numeric/ublas/matrix_proxy.hpp>
> >
> > int main() {
> > using namespace boost::numeric::ublas ;
> >
> > matrix< double > a(1,1);
> > const matrix< double > ca( a );
> >
> > matrix_row< matrix< double > > row_a( a, 0 );
> > matrix_row< const matrix< double > > row_ca( ca, 0 );
> > const matrix_row< const matrix< double > > crow_ca( ca, 0 );
> >
> > double b = row_a[0]; // compiles OK
> > double cb = row_ca[0]; // COMPILE ERROR
> > double ccb = crow_ca[0]; // compiles OK
> >
> >
> > return 0 ;
> > }
>
> This could be a difficult problem AFAIK. I'll try to analyze it later.

OK, I looked into it. You try to get a non-const reference to an element of
an const matrix: this should not work.

The only workaround I currently see to compile the program is to write

double cb = const_cast<const matrix_row< const matrix< double > > &>
(row_ca) [0];

instead of

double cb = row_ca[0]; // COMPILE ERROR

Regards

Joerg


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk