Hi,
I want to parametrize the access to my 'valarray' data member with Accessor functors, so that clients of my library can use a callback to their own access function. These accessors just need to transform the row- and column-index to one overall index.
For this purpose, I have made an interface class "MatrixAccessor":
Code:
class MatrixAccessor
{
public:
virtual size_t operator()(size_t i, size_t j);
};
with two derivations
Code:
class C_Accessor : public MatrixAccessor // for row-oriented memory access
{
public:
C_Accessor(size_t nb_rows, size_t nb_cols) : _nbrows(nb_rows), _nbcols(nb_cols) {}
size_t operator()(size_t i, size_t j)
{
return(i+j*_nbrows);
}
private:
size_t _nbrows;
size_t _nbcols;
};
However, when I want to pass this Accessor via a template parameter, like in
Code:
template<typename T, typename Accessor = C_Accessor>
class Matrix
{
public:
MatrixAccessor get_accessor() const {
return this->static_cast<MatrixAccessor>(accessor);
}
};
I will have to specialise this parameter in ALL member functions! And that's not exactly what will make my library user-friendly.
Main problem: today's C++ standard does not allow default-specialisation of function template parameters.
As a workaround, my idea is to not store the function object as an "Accessor" (template parameter), but as a "MatrixAccessor" (interface). I now pass the accessor as argument in the constructors, with as default value a static const object which I declare in the same header as the accessor declarations. However, this does not allow the interface from being an abstract base class, which I initially wanted.
But, I do have the feeling that I'm not benefitting from static polymorphism as much as I could, and that this workaround will slow down algorithms with my library.
Has someone got a better idea, or improvements to my idea?
Thanks a lot!!!
edit:
The constructor argument will be a pointer to MatrixAccessor, of course necessary to call the correct 'operator()'. The base class can now remain a pure abstract class.