Table of pointers to templated functions

From: Thomas Matthews (Thomas_MatthewsSpitsOnSpamBots_at_sbcglobal.net)
Date: 07/28/04


Date: Wed, 28 Jul 2004 18:49:19 GMT

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::ostream;
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[i](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


Relevant Pages

  • Re: Pointer Indirection Syntax
    ... we have a need for a pointer to a pointer ... >> to a function returning void and having void parameters. ... >> What is the syntax for declaring a pointer to a pointer ... typedef void; ...
    (comp.lang.c)
  • Re: Trying to Retrieve a List of Active Serial/Com Ports
    ... typedef PVOID HANDLE; ... So HANDLE is a void* or a PVOID, which I would -guess- is just ... HANDLE is probably a pointer. ... trying to store a pointer in an int. ...
    (comp.lang.c)
  • Re: Question about C code and use of "void"
    ... declares a variable called fnptr that is a pointer to a function that ... takes an int argument and returns void, ... pointer to a function that takes an int argument and returns void. ... A typedef can also be used to define a function type. ...
    (microsoft.public.vc.language)
  • Re: Help with STL allocators
    ... that it is ignoring the void specialised version of pool_allocator. ... template <typename T> ... typedef ptrdiff_t difference_type; ...
    (comp.lang.cpp)
  • Re: Please Can Anny One Tell What This Declaration Says ?
    ... FP is a typedef (synonym) for pointer to void. ... UserMenuFunck and UserFunk are each an array of 7 pointer to void. ...
    (comp.lang.c)