Hi,
I want to make the two functions to acces the matrix elements in a valarray user-modifyable by using a callback to a functor, which is passed as class-template parameter and instanciated as the data member "accessor" in the class. These are the functions 'at', two versions for const-correctness:
Code:
template<typename T, typename AccessorT>
inline T Matrix<T,AccessorT> :: at(size_t const& i, size_t const& j) const
{
if(i>=this->nb_rows() || j>=this->nb_cols()) {
throw MatrixException("index out of bounds");
}
return this->_elms[this->accessor(i,j)]; // <<----------------- use of the accessor
}
template<typename T, typename AccessorT>
inline T& Matrix<T,AccessorT> :: at(size_t const& i, size_t const& j)
{
if(i>=this->nb_rows() || j>=this->nb_cols()) {
throw MatrixException("index out of bounds");
}
return this->_elms[this->accessor(i,j)]; // <<----------------- use of the accessor
}
The functor "accessor" simply translates the two indices 'i,j' in one index 'k' . I have a default accessor defined as:
Code:
class MatrixAccessor
{
public:
virtual size_t operator()(size_t const& i, size_t const& j) = 0;
virtual ~MatrixAccessor(){}
};
class C_Accessor : public MatrixAccessor // for row-oriented memory access
{
public:
C_Accessor() : _nbrows(0), _nbcols(0) {}
C_Accessor(size_t const& nb_rows) : _nbrows(nb_rows), _nbcols(nb_rows) {}
C_Accessor(size_t const& nb_rows, size_t const& nb_cols) : _nbrows(nb_rows), _nbcols(nb_cols) {}
size_t operator()(size_t const& i, size_t const& j)
{
return(i+j*_nbrows);
}
private:
size_t _nbrows;
size_t _nbcols;
};
My problem is const-correctness :
error C3848: expression having type 'const LinearAlgebra::C_Accessor' would lose some const-volatile qualifiers in order to call 'size_t LinearAlgebra::C_Accessor:: operator ()(const size_t &,const size_t &)'
Is this due to the operator[] of the valarray class? Should the operator() of the functor return a 'const size_t' ?
I haven't found the operator[] of the valarray class in the documentation, by the way...
http://publib.boulder.ibm.com/infoce...r_valarray.htm
http://msdn.microsoft.com/en-us/libr...y8(VS.80).aspx
Thanks a lot for your help!