Suppose I have a datastructure (actually it's a graph) with one template
parameter (the property P for each edge and vertex):
struct graph<P>;
struct vertex<P>;
struct edge<P>;
I also have an algorithm that modifies this datastructure. The basic
outline of the algorithm is independent of the type of property. So I
implemented a generic version of the algorithm and a function object for
each type of property I need. In this way, adding a new property
involves simply defining the property and implementing the function
object. And that works great, however I have a question about the
function object, which looks more or less like this:
template <typename P>
struct function_object {
// Initialization
void initialize(regi on<P>& r) const;
void initialize(edge <P>& e) const;
// Evaluation
double evaluate(const edge<P>& e) const;
};
As you can see I declared it with a template parameter (and add no
implementation) . For each type of property I add a specialization (with
an implementation of course).
struct my_property;
template <>
struct function_object <my_property{
// Initialization
void initialize(regi on<my_property> & r) const;
void initialize(edge <my_property> & e) const;
// Evaluation
double evaluate(const edge<my_propert y>& e) const;
};
But I could as well have done this without using "function_objec t". Are
there pros and/or contras against this design? Initially, my idea was to
add only a specialization of the member functions only, and not the
whole class, but that doesn't work.
parameter (the property P for each edge and vertex):
struct graph<P>;
struct vertex<P>;
struct edge<P>;
I also have an algorithm that modifies this datastructure. The basic
outline of the algorithm is independent of the type of property. So I
implemented a generic version of the algorithm and a function object for
each type of property I need. In this way, adding a new property
involves simply defining the property and implementing the function
object. And that works great, however I have a question about the
function object, which looks more or less like this:
template <typename P>
struct function_object {
// Initialization
void initialize(regi on<P>& r) const;
void initialize(edge <P>& e) const;
// Evaluation
double evaluate(const edge<P>& e) const;
};
As you can see I declared it with a template parameter (and add no
implementation) . For each type of property I add a specialization (with
an implementation of course).
struct my_property;
template <>
struct function_object <my_property{
// Initialization
void initialize(regi on<my_property> & r) const;
void initialize(edge <my_property> & e) const;
// Evaluation
double evaluate(const edge<my_propert y>& e) const;
};
But I could as well have done this without using "function_objec t". Are
there pros and/or contras against this design? Initially, my idea was to
add only a specialization of the member functions only, and not the
whole class, but that doesn't work.
Comment