472,350 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

generic programming and dynamic polymorphism


Cracow, 20.10.2004

Hello,

As far as I understand, the generic programming basically consists
in using templates for achieving a static polymorphism of the
various code fragments, and their reuse for various template
parameters. I wonder if there exist techniques for achieving
a dynamic polymorphism using the generic programming. Is this
possible? If yes, can anyone show me simple examples in C++
of how this can be done?

Sincerely,

L.B.
*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Electrochemical Oxidation of Gaseous Fuels, |
| ul. Zagrody 13, 30-318 Cracow, Poland. |
| tel./fax: +48 (12) 266-03-41 |
| E-mail: nb******@cyf-kr.edu.pl |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
Jul 22 '05 #1
4 4297
Leslaw Bieniasz wrote:
Cracow, 20.10.2004

Hello,

As far as I understand, the generic programming basically consists
in using templates for achieving a static polymorphism of the
various code fragments, and their reuse for various template
parameters. I wonder if there exist techniques for achieving
a dynamic polymorphism using the generic programming. Is this
possible? If yes, can anyone show me simple examples in C++
of how this can be done?


It all depends on how narrowly you define "generic programming". Do you
restrict it to compile-time constructs like templates, or do you broaden
the definition to encompass other methods of maintaining a consistent
interface across many different actual types.

Virtual functions are the standard way of implementing dynamic
polymorphism, but templates can indeed be a big help. Consider the
example of a "callback" template.

class adapter_base{
public:
virtual ~adapter_base(){}
virtual void operator()() = 0;
};

template<typename T>
class adapter: public adapter_base{
T& object;
public:
explicit adapter(T& t): object(t){}
virtual void operator()(){ object(); }
};
class callback_executer{
std::vector<adapter_base*> callbacks;
public:
template<typename T>
void add_callback(const T& t){
callbacks.push_back(new adapter(t));
}
void execute_all(){
for (std::vector<adapter_base*>::iterator i = callbacks.begin();
i != callbacks.end();
++i)
{
(*(*i))();
}
}
~callback_executer(){
for (std::vector<adapter_base*>::iterator i = callbacks.begin();
i != callbacks.end();
++i)
{
delete *i;
}
}
};
struct callback_A{
void operator()(){
std::cout << "callback A executed!\n";
}
};

struct callback_B{
void operator()(){
std::cout << "callback B executed!\n";
}
};

int main(){
callback_executer c;
c.add_callback(callback_A());
c.add_callback(callback_B());
c.execute_all();
}
Regards,
Jacques.
Jul 22 '05 #2
Jacques Labuschagne wrote:
Leslaw Bieniasz wrote:
Cracow, 20.10.2004

Hello,

As far as I understand, the generic programming basically consists
in using templates for achieving a static polymorphism of the
various code fragments, and their reuse for various template
parameters. I wonder if there exist techniques for achieving
a dynamic polymorphism using the generic programming. Is this
possible? If yes, can anyone show me simple examples in C++
of how this can be done?

It all depends on how narrowly you define "generic programming". Do you
restrict it to compile-time constructs like templates, or do you broaden
the definition to encompass other methods of maintaining a consistent
interface across many different actual types.

Virtual functions are the standard way of implementing dynamic
polymorphism, but templates can indeed be a big help. Consider the
example of a "callback" template.


I've made some corrections below, which do not diminish the value of
the example, only fix some bugs to make it compilable.

class adapter_base{
public:
virtual ~adapter_base(){}
virtual void operator()() = 0;
};

template<typename T>
class adapter: public adapter_base{
T& object;
public:
explicit adapter(T& t): object(t){}
virtual void operator()(){ object(); }
};


#include <vector>
class callback_executer{
std::vector<adapter_base*> callbacks;
public:
template<typename T>
void add_callback(const T& t){
void add_callback(T& t) {
callbacks.push_back(new adapter(t));
callbacks.push_back(new adapter<T>(t));
}
void execute_all(){
for (std::vector<adapter_base*>::iterator i = callbacks.begin();
i != callbacks.end();
++i)
{
(*(*i))();
}
}
~callback_executer(){
for (std::vector<adapter_base*>::iterator i = callbacks.begin();
i != callbacks.end();
++i)
{
delete *i;
}
}
};

#include <iostream>

struct callback_A{
void operator()(){
std::cout << "callback A executed!\n";
}
};

struct callback_B{
void operator()(){
std::cout << "callback B executed!\n";
}
};

int main(){
callback_executer c;
c.add_callback(callback_A());
c.add_callback(callback_B());
Can't use temporaries. Have to declare real objects.

callback_A a;
c.add_callback(a);
callback_B b;
c.add_callback(b);

Otherwise, the code has to be severely const-corrected.
c.execute_all();
}


V
Jul 22 '05 #3
Victor Bazarov wrote:
I've made some corrections below, which do not diminish the value of
the example, only fix some bugs to make it compilable.


Thanks. :-)
Jacques.
Jul 22 '05 #4

Cracow, 21.10.2004

Hello,

On Wed, 20 Oct 2004, Jacques Labuschagne wrote:
As far as I understand, the generic programming basically consists
in using templates for achieving a static polymorphism of the
various code fragments, and their reuse for various template
parameters. I wonder if there exist techniques for achieving
a dynamic polymorphism using the generic programming. Is this
possible? If yes, can anyone show me simple examples in C++
of how this can be done?


It all depends on how narrowly you define "generic programming". Do you
restrict it to compile-time constructs like templates, or do you broaden
the definition to encompass other methods of maintaining a consistent
interface across many different actual types.

Virtual functions are the standard way of implementing dynamic
polymorphism, but templates can indeed be a big help. Consider the
example of a "callback" template.

What I have in mind is whether one can avoid using virtual functions,
but achieve the same effect of dynamic polymorphism using only templates.
The virtual functions allow one to differentiate the behaviour of objects
depending on the dynamic, run-time type of the object. However, in
principle the types of all objects created within a program are always
exactly known at the moment of their creation. Therefore, one might
expect (at least theoretically) that if this static, compile-time type
information could be passed somehow to the code that calls the object
methods, then one could avoid using the virtual functions, but obtain
the same polymorphic behaviour. This might be pretty complicated to
do, I suppose.

My question is motivated by my attempts to understand some
existing libraries, like the matrix algebra library MTL, which
is said to use generic programming and templates. Various types
of vectors/matrices are there defined as templates, each one for
one matrix/vector type, and dynamic polymorphism is avoided (I may
be wrong though). Therefore, there does not seem to be anything
like a base matrix or vector class, from which all types are derived.
I therefore do not see a way to write polymorphic constructs
such as for example

Add(a,b,c)

which could perform addition c = a+b

of various types of vectors or matrices, assuming that a,b and c
are pointers or references to the base type. Instead, one always
have to specify to which particular types the a, b and c point/refer
to, whenever a,b and c are declared. This appears a severe limitation
to me, because various parts of the code must always be specialised,
or occur as templates with many parameters (representing, for example the
types of a, b, and c in the above example). Therefore, I am asking if
there exist techniques that would allow me to achieve dynamic polymorphism
without using class hierarchies with virtual functions.

L.B.
*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Electrochemical Oxidation of Gaseous Fuels, |
| ul. Zagrody 13, 30-318 Cracow, Poland. |
| tel./fax: +48 (12) 266-03-41 |
| E-mail: nb******@cyf-kr.edu.pl |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
Jul 22 '05 #5

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

Similar topics

18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a...
65
by: Roger Smythe | last post by:
A means for the progressive decomposition a problem space into increasingly simpler component parts such that these component parts represent higher...
15
by: rwf_20 | last post by:
I just wanted to throw this up here in case anyone smarter than me has a suggestion/workaround: Problem: I have a classic producer/consumer...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++...
25
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to...
9
by: Royt | last post by:
Maybe it's a stupid question, but I really couldn't figure it out. C++ is expected to be a better C, provides compatibility with C, and is able to...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic...
3
by: Johs | last post by:
I have read that when you are using templates you are making generic programs. But I don't see whats so generic about templates. You can make...
11
by: Vitor | last post by:
I have been writing some large programs and games lately and I have had people advise me that I need to write more object oriented programs. I...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.