Re: [eigen] Is it really impossible to modify a column of a matrix with in function? |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
On 08/30/2011 12:35:15 PM, Christoph Hertzberg wrote:
> On 30.08.2011 11:58, Helmut Jarausch wrote:
> > void set_Col(VectorXi& V) {
> > V[0]=1;
> > }
> >
> > int main() {
> > MatrixXi A(2,3);
> > A<< 1,2,3,
> > 4,5,6;
> > set_Col(A.col(1));
> > std::cout<< "A=\n"<< A<< std::endl;
> >
> > }
> >
> >
> > It looks as if a column cannot be passed by a non-const reference.
> > Why doesn't the col-method return a non-const reference for a non-
> > const object (the col-method could be overloaded with/without the
> > const attribute)?
>
> That's because A.col(1) does not return a VectorXi but a view on the
> corresponding column. Unfortunately passing this to another function
> is
> a bit hackish. To stay generic you need to pass a
> MatrixBase<Derived>&
>
> and because A.col() returns a temporary it needs to be a const-ref,
> which you need to const-cast within your method.
> Look here for details:
> http://eigen.tuxfamily.org/dox/
> TopicFunctionTakingEigenTypes.html#TopicPlainFunctionsFailing
Many thanks!
It would be nice if there were a proxy class doing this for me.
E.g. within my own (dated) matrix/vector class I had proxy classes
Vector_L and Matrix_L which were used as return values and which can be
passed as const reference. All write-access operators, like operator[],
operator>> and operator== do const casting internally.
Helmut.