L
Luc Claustres
I have a generic container such as:
template<class T>
class Container
{
// some data structure that store elements of type T
}
I use this container in a hierarchical manner,
that is elements can be themselves containers:
Container<Container<float>> lMyContainer;
What I want to do is to call a method on the
container that has to be recursively called
on its elements if they are not simple type
but container themselves.
The best idea I've got for the moment is to
declare a template "operator" called by the
container and specialized for simple types:
template<class T>
struct Operator
{
static void f(T& element) { element.f(); }
}
template<>
struct Operator
{
static void f(float& element) { // Do nothing }
}
Then in the container I do:
template<class T>
void f()
{
// Perform computation on the elements
...
// Then call the method on the elements
Iterate on each element E
Operator<T>::f(E);
}
I'd like something more elegant and which
behaviour may be changed dynamically by the user
at execution time (not at compilation).
I think this could be done through functors
but I did not find the solution.
Thanks in advance.
Luc Claustres
template<class T>
class Container
{
// some data structure that store elements of type T
}
I use this container in a hierarchical manner,
that is elements can be themselves containers:
Container<Container<float>> lMyContainer;
What I want to do is to call a method on the
container that has to be recursively called
on its elements if they are not simple type
but container themselves.
The best idea I've got for the moment is to
declare a template "operator" called by the
container and specialized for simple types:
template<class T>
struct Operator
{
static void f(T& element) { element.f(); }
}
template<>
struct Operator
{
static void f(float& element) { // Do nothing }
}
Then in the container I do:
template<class T>
void f()
{
// Perform computation on the elements
...
// Then call the method on the elements
Iterate on each element E
Operator<T>::f(E);
}
I'd like something more elegant and which
behaviour may be changed dynamically by the user
at execution time (not at compilation).
I think this could be done through functors
but I did not find the solution.
Thanks in advance.
Luc Claustres