Table of pointers to templated functions

T

Thomas Matthews

Hi,

I would like to create a table (or vector) of pointers
to templated functions.

1. How do I declare a typedef of a pointer to a templated
function?

For example, I have some functions that print out a
type's name to an istream:

#include <iostream>
#include <string>
#include <cstdlib> // using EXIT_SUCCESS
using std::eek:stream;
using std::endl;
using std::cout;
using std::string;

/* The generic function template */
template <typename AnyType>
void print_name(ostream& out)
{
AnyType t;
t.print_name(out);
return;
}

/* Specializations of the generic function template */
template<>
void print_name<int>(ostream& out)
{
out << "integer";
return;
}


template<>
void print_name<double>(ostream& out)
{
out << "double";
return;
}


template<>
void print_name<std::string>(ostream& out)
{
out << "std::string";
return;
}


/* The typedef for the function,
here's my guess:
*/
typedef void (*P_Print_Name)(ostream& out);


/* A "User class" */
class My_Class
{
public:
void print_name(ostream& out) const
{ out << "My_Class"; }
};


/* A table of function pointers */
const P_Print_Name table[] =
{
print_name<int>, print_name<double>,
print_name<string>,
print_name<My_Class>
};
const unsigned int NUM_FUNCTIONS =
sizeof(table) / sizeof(table[0]);


/* The Driver */
int main(void)
{
for (unsigned int i = 0;
i < NUM_FUNCTIONS;
++i)
{
table(cout);
cout << '\n';
}
cout.flush();
return EXIT_SUCCESS;
}


This is a simple illustration of what I want to do:
iterate through a table of pointers to templated
functions. I'm writing test functions and each
function has an input file and a pointer to a
window. The driver will pass an input file and
a pointer to a window to each function. There will
be functions for various objects, but each function
has the same parameters and return type.

The typedef will make declaring the table or vector
easier (to type and read).

So do I have the typedef correct?
If not, what is the proper syntax?


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
V

Victor Bazarov

Thomas said:
I would like to create a table (or vector) of pointers
to templated functions.

Only pointers to real functions can exist. That means that
you need to instantiate the function templates before taking
the address of each.
1. How do I declare a typedef of a pointer to a templated
function?

No such thing.
For example, I have some functions that print out a
type's name to an istream:

#include <iostream>
#include <string>
#include <cstdlib> // using EXIT_SUCCESS
using std::eek:stream;
using std::endl;
using std::cout;
using std::string;

/* The generic function template */
template <typename AnyType>
void print_name(ostream& out)
{
AnyType t;
t.print_name(out);
return;
}

/* Specializations of the generic function template */
template<>
void print_name<int>(ostream& out)
{
out << "integer";
return;
}


template<>
void print_name<double>(ostream& out)
{
out << "double";
return;
}


template<>
void print_name<std::string>(ostream& out)
{
out << "std::string";
return;
}


/* The typedef for the function,
here's my guess:
*/
typedef void (*P_Print_Name)(ostream& out);

Good guess. 'P_Print_Name' is a pointer to a function that
takes one argument of type ostream& and returns nothing.
/* A "User class" */
class My_Class
{
public:
void print_name(ostream& out) const
{ out << "My_Class"; }
};


/* A table of function pointers */
const P_Print_Name table[] =
{
print_name<int>, print_name<double>,
print_name<string>,
print_name<My_Class>
};
const unsigned int NUM_FUNCTIONS =
sizeof(table) / sizeof(table[0]);


/* The Driver */
int main(void)
{
for (unsigned int i = 0;
i < NUM_FUNCTIONS;
++i)
{
table(cout);
cout << '\n';
}
cout.flush();
return EXIT_SUCCESS;
}


This is a simple illustration of what I want to do:
iterate through a table of pointers to templated
functions. I'm writing test functions and each
function has an input file and a pointer to a
window. The driver will pass an input file and
a pointer to a window to each function. There will
be functions for various objects, but each function
has the same parameters and return type.

The typedef will make declaring the table or vector
easier (to type and read).

So do I have the typedef correct?


What does your compiler say?
If not, what is the proper syntax?

I don't see any problems, does your compiler complain?

Try not to waste your own time by using the newsgroup as
a remote compilation device. If you have some code and
_your_ compiler doesn't want to compile it, post questions,
post the code, post error messages, etc.

V
 
D

David Hilsee

Thomas Matthews said:
Hi,

I would like to create a table (or vector) of pointers
to templated functions.

1. How do I declare a typedef of a pointer to a templated
function?

If I were you, I'd consider ignoring function pointers. In my experience,
virtual member functions will almost always offer exactly the same
functionality that most programmers want from function pointers, without all
of the potential confusion that can arise from using function pointers. As
a bonus, you can add state to the objects. Function pointers have their
place, but, in most cases, they are not necessary. However, I do agree with
Victor that you should have provided more information indicating the exact
problem that you are having.
For example, I have some functions that print out a
type's name to an istream:

#include <iostream>
#include <string>
#include <cstdlib> // using EXIT_SUCCESS
using std::eek:stream;
using std::endl;
using std::cout;
using std::string;

/* The generic function template */
template <typename AnyType>
void print_name(ostream& out)
{
AnyType t;
t.print_name(out);
return;
}

/* Specializations of the generic function template */
template<>
void print_name<int>(ostream& out)
{
out << "integer";
return;
}

If you were to follow my suggestion, then you could translate that into the
following:

struct Printer {
virtual void print_name(ostream& out) = 0;
};

template <typename AnyType>
struct GenericPrinter : Printer {
void print_name(ostream& out) {
AnyType t;
t.print_name(out);
}
};

// etc

You could then create a table of Printer objects by dynamically allocating
instances of the derived classes and storing them in a container that
contains pointers to Printer objects. If you find the above code easier to
read, then consider using that strategy. If not, then feel free to ignore
my suggestion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top