Generic Functor

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
 
C

Chris Theis

Luc Claustres said:
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

The problem with specialization is that you might have to cover a rather
wide range of possibilities. A better solution would be to think about a
functor which differentiates between types and containers with these types.
For example:

template<typename T>
class CFunctor {
public:
void operator() ( T& Val ) {
// treat the type }

void operator()( CMyContainer<T>& Cont ) {
// treat the container
}
};

HTH
Chris
 
C

Chris Theis

Luc Claustres said:
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

The problem with specialization is that you might have to cover a rather
wide range of possibilities. A better solution would be to think about a
functor which differentiates between types and containers with these types.
For example:

template<typename T>
class CFunctor {
public:
void operator() ( T& Val ) {
// treat the type }

void operator()( CMyContainer<T>& Cont ) {
// treat the container
}
};

HTH
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top