Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Table of pointers to templated functions

Reply
Thread Tools

Table of pointers to templated functions

 
 
Thomas Matthews
Guest
Posts: n/a
 
      07-28-2004
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[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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-28-2004
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: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[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?


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
 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      07-29-2004
"Thomas Matthews" <(E-Mail Removed)> wrote in
message newsySNc.518$(E-Mail Removed). ..
> 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: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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling templated member of templated object david@sunlightd.com C++ 1 06-22-2007 05:13 PM
templated function as parameter of another templated function Amadeus W. M. C++ 2 07-04-2006 09:59 PM
ASP.NET Templated User Controls - Limit child controls allowable within a templated control JohnyStyles@gmail.com ASP .Net 0 05-29-2006 06:00 PM
Subtypes of templated types (in templated functions) Marijn C++ 5 02-13-2004 09:50 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments