473,387 Members | 1,517 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,387 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 4390
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 real-time c++ project, and we're having a discussion...
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 levels of conceptual abstraction, and are...
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 system which accepts 'commands' from a socket and...
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++ Templates: The Complete Guide" by David Vandevoorde and...
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 this? I am fairly new to generics, but I am...
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 do all that C can do. however, the number of...
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 TypeConvert class I want to write, I have a call to...
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 generic programs without templates through the use of...
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 also I understand the concept and have written some...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.