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: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
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: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