473,415 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,415 software developers and data experts.

Table of pointers to templated functions

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

Jul 22 '05 #1
2 1992
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::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);
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
Jul 22 '05 #2
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:Py***************@newssvr33.news.prodigy.com. ..
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::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;
}


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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Thomas Matthews | last post by:
Hi, How do I create a const table of pointers to member functions? I'm implementing a Factory pattern (or jump table). I want to iterate through the table, calling each member function until a...
4
by: Sat | last post by:
Hi, I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
4
by: __PPS__ | last post by:
suppose I have class that has templated operator. I want to have a few overloads/template specializations for different types. Basicly, the biggest problem I have is to be able to have this: ...
2
by: DirtyClamDigger | last post by:
Hi Everyone: I'm trying to develop a property list to include as metadata about my object classes. i.e. I want each class I'm developing to include a PropertyList which will contain ObjectProperty...
5
by: Gert Van den Eynde | last post by:
Hi all, It's probably trivial but I can't figure it out... I have a templated class template <typename T, typename uclass A I wish to fill a vector with pointers to objects of class A. I...
22
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my...
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
13
by: edyam | last post by:
I'm trying to template a pointer-based list class and several functions (the ones in which I try to return a pointer) are producing error messages. The class: template <typename T> class List...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.