From: Joerg Walter (jhr.walter_at_[hidden])
Date: 2002-10-07 14:39:36


----- Original Message -----
From: "Toon Knapen" <toon.knapen_at_[hidden]>
To: "Boost mailing list" <boost_at_[hidden]>
Sent: Monday, October 07, 2002 5:14 PM
Subject: Re: [boost] ublas matrix_row<>::operator[] compile error

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

This would be easier for me at least ;-)

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

Yes, it look as if this could be possible. I've just checked your snippet
with

typedef typename detail::ct_if<boost::is_const<M>::value, typename
M::const_reference, typename M::reference>::type reference;

instead of

typedef typename M::reference reference;

and the sample compiles. I'll have to investigate, if any other codes break,
though.

Regards

Joerg