<jo**********@hotmail.comwrote in message news:55**********************************@i12g2000 prf.googlegroups.com...
On Nov 23, 9:49 am, Matthias Buelow <m...@incubus.dewrote:
>johnbrown...@hotmail.com wrote:
Is there a way to call f with an int parameter so that the generic
f<Tis called instead of the f<intspecialisation?
There is no f<T>, a template is kind of a compiler macro, the compiler
generates functions only if you instantiate a template (expand the
macro, in other words).
I understand that. Consider the output of the program that I posted:
overloaded void f(int j) called: j == 1
specialization void f<int>(int i) called: i == 2
specialization void f<int>(int i) called: i == 3
template<Tvoid f(T t) called: t == 4
Is there a way to write a call to f(5), so that the output would be:
template<Tvoid f(T t) called: t == 5
Never mind the semantic accuracy or elegance (or lack thereof!) of
what I wrote. If this is what we are aiming for, I suppose the
question
would be:
Is there a way to make the compiler generate its own instantiation,
rather than use the explicit one that I provided? When asked this way,
it would seem that the answer would be no, because if it generated its
own instantiation, it would be a duplicate symbol, because mine
already
exists.
Still, one never knows if the Powers That Be thought of some clever
reason to allow this, that mere mortals such as myself could not.
That's why I'm asking.
Now suppose you have some sort of container class called
dweeblist that is filled with dweeb objects. A pointer to the
current dweeb object is produced with member function current().
You can define a function that applies a member function
(which is passed as an argument) to all dweeb objects in dweeblist
and then call this function for dweeblist dl like this:
#include <iostream.h>
class dweeb {
public:
void spin () {cout << "spinning!" << endl; }
void dodge () {cout << "dodging!" << endl; }
void feint () {cout << "feinting!" << endl; }
};
template<int szclass dweeblist {
dweeb list [sz];
int cursor;
public:
dweeblist () : cursor(0) {}
void reset () { cursor = 0; }
dweeb * current () { return &list [cursor]; }
void next () { cursor++; }
int end () { return cursor >= sz; }
void apply (void (dweeb::* df) () ) {
reset ();
while ( !end() ) {
(current ()->*df) ();
next ();
}
}
};
void main () {
dweeblist<7dl;
dl.apply(&dweeb::spin);
dl.apply(&dweeb::dodge);
dl.apply(&dweeb::feint);
}
Typically, the kind of function you'd want to apply()
to every object in a list would be something like draw().
for CAD.
Pointers to members have fairly limited use, but they
can be quite helpful when you want to delay the selection
of a specific function until the program is running.
C++ Inside & Out
Bruce Eckel