class - Implementing the A(:,k)=b; Matlab-like syntax in a C++ matrix library: avoiding const-ness -
i have developed expression templates-based c++ matrix class of own. have implemented range class enable matlab-like reads as
cout << a(range(3,5),range(0,10)) << endl; i enable matlab-like assignments as
a(range(3,5),range(0,10))=b; where b appropriate matrix.
the matrix () operator overload follows
inline expr<submatrixexpr<const outtype*,outtype>,outtype> operator()(range range1, range range2) { typedef submatrixexpr<const outtype*,outtype> sexpr; return expr<sexpr,outtype>(sexpr(...some stuff...),...some stuff...); } the submatrixexpr class exemplified
template <class a, class type> class submatrixexpr { // constructor (m pointer matrix data) submatrixexpr(const &m, ...stuff...) : ...stuff... // access operator inline type operator[](const int i) const { ...stuff... } } while expr class exemplified follows:
template <class a, class b> class expr { // constructor (a expression, submatrixexpr in case) expr(const &a, ...stuff...) : ...stuff... // access inline b operator[](const int i) const { return a_[i]; } expr<a,b> operator=(const matrix<b> &ob) { (int i=0; i<getnumelements(); i++) { std::cout << a_[i] << " " << ob.getdatapointer()[i] << "\n"; a_[i] = ob.getdatapointer()[i]; } return *this; } } my problem following. use const in access operators 2 expression classes above. result overloaded = operator of expr class correctly returns a_[i] , ob.getdatapointer()[i], not make assignment.
is possible disregard const-ness within overloaded = operator without having change entire code?
thank help.
edit following lol4t0's answer
i have removed original access operator expr class , added
inline const b& operator[](const int i) const { return a_[i]; } inline b& operator[](const int i) { const expr& constthis = *this; return const_cast<b&>(constthis[i]); } also, have removed original access operator submatrixexpr , added
inline const type& operator[](const int i) const { // stuff return m_[idx2r(globalrow,globalcolumn,columns_up_)]; } and
inline type& operator[](const int i) { // stuff // following line returns error return m_[idx2r(globalrow,globalcolumn,columns_up_)]; } unfortunately, compiler returns following error
qualifiers dropped in binding reference of type "librarynamespace::double2_ &" initializer of type "const librarynamespace::double2_" (double2_ complex type of own).
edit #2 - information on m_
template <class a, class type> class submatrixexpr { private: m_; // stuff } from matrix () operator overload reported above, a = const outtype*, outtype matrix type, double2_ example i'm running.
that not constenss problem. return by value in operator []'s. so, returned value gets copied , assign new value copy, gets destroyed.
actually, code works this
struct s { int v; } x = {0}; s foo() { return x;} int main() { foo() = {1}; std::cout << foo().v; } of cause, not save new value, assigned.
usually containers have 2 overloads of operator[]:
the first overload operates on constant container , returns constant reference.
const type& operator[](const int i) const { ...stuff... } const b& operator[](const int i) const { return a_[i]; }second overload works mutable container , returns by reference, container items modified:
type& operator[](const int i) { ...stuff... } b& operator[](const int i) { return a_[i]; }
in principle, mutable version can implemented through constant one:
b& class::operator[](const int i) { const class& constthis = *this; return const_cast<b&>(constthis[i]); }
Comments
Post a Comment