This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Const cast does not apply anymore?

Tags: None
(comma "," separated)
Gabyx
Registered Member
Posts
10
Karma
0

Const cast does not apply anymore?

Tue May 17, 2011 2:58 pm
I tried the following. I want to change a reference of MatrixBase in a function...

Code: Select all

...
template<typename Derived>
void foo(Eigen::MatrixBase<Derived> &output){
   output.setConstant(10);

}
...

Eigen::Matrix<double,6,6> gg;
   gg.setConstant(4);
   foo(gg.block<4,4>(0,0).diagonal().segment<2>(0));
   cout << gg <<endl;


Output:
10 4 4 4 4 4
4 10 4 4 4 4
4 4 4 4 4 4
4 4 4 4 4 4
4 4 4 4 4 4
4 4 4 4 4 4
Whih works!

Why should I use the following (const_cast) as suggested in the documentation http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html#TopicPlainFunctionsFailingto use:
Code: Select all
template<typename Derived>
void foo(const Eigen::MatrixBase<Derived> &output){
   // do here a const_cast
}


Functions taking writable (non-const) parameters must take const references and cast away constness within the function body.

Its not needed to change a reference... ?? I dont understand...

Thanks for your help!!
Gabyx
Registered Member
Posts
10
Karma
0

[SOLVED]

Wed May 18, 2011 7:51 am
Only MSVC allows to assign rvalues to non-const reference, silently, without noting!....

Thats why it worked,

so in general use CONST!


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], q.ignora, watchstar