Boost logo

Boost :

From: Toon Knapen (toon.knapen_at_[hidden])
Date: 2002-10-07 10:14:18


On Sunday 06 October 2002 19:14, Joerg Walter wrote:
> > ----- 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.

But "matrix_row< const matrix< double > >" is not equivalent to "matrix_row<
matrix< double > >". And, if I understand you correctly, you are saying that
the former is an impossible combination because the matrix_row should be made
explicitly const also if it's original matrix is const.

>
> > > #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.

I know, but what I'm saying is : can't a
matrix_row< const matrix< double > >::reference end up as being a const
matrix< double >::reference automatically.

AFAIK, this can be done easily with a traits class that has a specialisation
for 'const' arguments.

>
> 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];
or better yet :
const matrix_row< const matrix< double > >& cb_const_ref = cb ;
cb_const_ref[0] ;


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