473,809 Members | 2,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Conta iner<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 1895

"Luc Claustres" <lu***********@ c-s.fr> wrote in message
news:b8******** *************** ***@posting.goo gle.com...
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<Conta iner<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<typena me 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.goo gle.com...
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<Conta iner<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<typena me 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
2257
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 standpoint, to make code as transparent as possible. through its lifecycle, code inevitably becomes larger and more complex to cope with unexpected demands. if the starting point looks like alexandrescu's code, it will require very specialized...
4
1708
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 different et lib from scratch. Or, maybe I was just ignorant and there is some already? matrix, string, array, vector, whatever, the idea of et is quite similar. We need a leaf node and a binary non-leaf node for expressing
8
1985
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. functor<bool (long, long)> myFunctor; myFunctor = AFunctionOfThatPrototype; To get that much is elementary because the template can just store a (bool)(long,long) as a member variable in functor<T>. Here is the problem:
4
2909
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)> myFunctor = SampleFunction; string result = myFunctor(7, true); Works great thanks to the help from this group. Here's the guts so far for two-arity:
12
1973
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 { ... double evaluate(){
3
1757
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; break; case Double: *(double*)p = 42; break;
2
2520
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 below). Or, rather, it works fine as long as my member functor is const. The problem comes when I wish to use it for a *non*-const functor (see listing 2 below): *** Start listing 1 *************************************************** // test1.cpp
5
2533
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 a multi-threaded application, naturally I want the call back functor to be created on a per thread basis. Suppose my thread is so defined template <typename servlet_type>
2
2293
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 ----nextSibling ---->0 | |
0
9722
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10643
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.