Table of pointers to templated functions

Discussion in 'C++' started by Thomas Matthews, Jul 28, 2004.

  1. 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
    Thomas Matthews, Jul 28, 2004
    #1
    1. Advertising

  2. Thomas Matthews wrote:
    > 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
    Victor Bazarov, Jul 28, 2004
    #2
    1. Advertising

  3. Thomas Matthews

    David Hilsee Guest

    "Thomas Matthews" <> wrote in
    message news:pySNc.518$...
    > 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.

    --
    David Hilsee
    David Hilsee, Jul 29, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    398
    RA Scheltema
    Jan 6, 2004
  2. Marijn
    Replies:
    5
    Views:
    456
    Marijn
    Feb 13, 2004
  3. Replies:
    0
    Views:
    2,227
  4. Amadeus W. M.
    Replies:
    2
    Views:
    391
    Amadeus W. M.
    Jul 4, 2006
  5. Replies:
    1
    Views:
    303
Loading...

Share This Page