J
Juha Nieminen
I tried to find tutorials about this in the web, but I couldn't find
any (every tutorial I found only gave a basic introduction to templates,
without going any deeper).
Assume that I have a class (let's call it A) with a (const) function
(taking no parameters) in it (let's say, for example, print()), a data
container containing instances of that class (let's call it v), and
that I want to create a function called ForEach() which I can call
like this:
ForEach(v, &A:rint);
I believe the easiest way of implementing this ForEach() function
would be:
template<typename Container, typename Function>
void ForEach(const Container& c, Function f)
{
for(typename Container::const_iterator iter = c.begin();
iter != c.end(); ++iter)
{
((*iter).*f)();
}
}
This works. The problem with it is that it can't be overloaded for
other purposes.
For example, assume that I want to make a version of the ForEach()
function which takes as second parameter a global/static function
which takes an A type const reference as parameter, so for example
if I had a function like "print(const A&)" I could do this:
ForEach(v, print);
In order to be able to overload ForEach() like this, the
implementation above would, AFAIK, have to use a more specific
version of the 'f' parameter than the generic 'Function' template
type. The Container element type would have to be specified
separately and the 'f' parameter of the function would have to
specify that it's a member function of that container element type
and not something else.
However, I just can't find the exact syntax for this anywhere.
Could somebody shed some light on this?
any (every tutorial I found only gave a basic introduction to templates,
without going any deeper).
Assume that I have a class (let's call it A) with a (const) function
(taking no parameters) in it (let's say, for example, print()), a data
container containing instances of that class (let's call it v), and
that I want to create a function called ForEach() which I can call
like this:
ForEach(v, &A:rint);
I believe the easiest way of implementing this ForEach() function
would be:
template<typename Container, typename Function>
void ForEach(const Container& c, Function f)
{
for(typename Container::const_iterator iter = c.begin();
iter != c.end(); ++iter)
{
((*iter).*f)();
}
}
This works. The problem with it is that it can't be overloaded for
other purposes.
For example, assume that I want to make a version of the ForEach()
function which takes as second parameter a global/static function
which takes an A type const reference as parameter, so for example
if I had a function like "print(const A&)" I could do this:
ForEach(v, print);
In order to be able to overload ForEach() like this, the
implementation above would, AFAIK, have to use a more specific
version of the 'f' parameter than the generic 'Function' template
type. The Container element type would have to be specified
separately and the 'f' parameter of the function would have to
specify that it's a member function of that container element type
and not something else.
However, I just can't find the exact syntax for this anywhere.
Could somebody shed some light on this?