473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Generic Functor

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
Jul 22 '05 #1
2 1871

"Luc Claustres" <lu***********@c-s.fr> wrote in message
news:b8**************************@posting.google.c om...
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
Jul 22 '05 #2

"Luc Claustres" <lu***********@c-s.fr> wrote in message
news:b8**************************@posting.google.c om...
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

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: gong | last post by:
hi i recently looked at alexandrescu's book on c++, and i found it pretty much unintelligible. i have a few points which i wonder about. 1. as a developer, it is important, from a bottom line...
4
by: Ben | last post by:
Hi, I was writing a expression template for string concatenation latetly. When writing it, I started to feel curious that why there's not any generic et lib that can save me from wring each...
8
by: daniel.w.gelder | last post by:
Hello, I have been trying to write a functor template for a week now and I'm just having tons of trouble because I don't understand an issue that I guess is pretty basic to this task. ...
4
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)>...
12
by: aaragon | last post by:
Hi everyone, I'm trying to provide some external functionality to a class through a functor object defined by the user. The concept is as follows: template <class Functor> class ClassA {...
3
by: Frederick Gotham | last post by:
For objects, we have "void*" as the generic pointer type. For instance: enum ParamType { Int, Double }; void Func(void *const p,ParamType const pt) { switch (pt) { case Int: *(int*)p = 42;...
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
5
by: Fei Liu | last post by:
Hello, I have a situation where I need to design a library for multi-thread application, each thread does some work in a client supplied std::ptr_fun(free_function) or a functor. Now since it's...
2
by: aaragon | last post by:
Hi guys, Is there a way to return a functor from a recursive call that takes different paths? Let's say that I have a tree structure like: root | first child ---- nextSibling ----nextSibling...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.