473,386 Members | 1,830 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,386 software developers and data experts.

pointer to template function

Is it possible to store a pointer to a template function?

eg,
template<class T> fooFunc() {...}

[ptr_to_template_function] fptr = fooFunc; // <--

I couldn't find any info here, not even in the forums:
http://www.function-pointer.org/

Thanks,
Suzanne

Jul 22 '05 #1
5 2920
Suzanne Vogel wrote:
Is it possible to store a pointer to a template function?

eg,
template<class T> fooFunc() {...}

[ptr_to_template_function] fptr = fooFunc; // <--

I couldn't find any info here, not even in the forums:
http://www.function-pointer.org/

Thanks,
Suzanne

What seems to be the problem ?

The example you start with has no dependance on the template.
I created a second example that might be more like what you're after ...
see below.

template<class T> int fooFunc()
{
return 0;
}

class x;

int (* ptr_fooFunc)() = &fooFunc<x>;
template<class T> T * zooFunc()
{
return 0;
}

template<class T>
struct Stuff
{

static T* (* ptr_zooFunc)();

};

template<class T>
T* (* Stuff<T>::ptr_zooFunc)() = &zooFunc<T>;

int main()
{
ptr_fooFunc();

(*Stuff<int>::ptr_zooFunc)();

}

Jul 22 '05 #2
On Fri, 16 Jan 2004 18:46:37 -0500 in comp.lang.c++, Suzanne Vogel
<su*************@hotmail.com> was alleged to have written:
Is it possible to store a pointer to a template function?

eg,
template<class T> fooFunc() {...}

[ptr_to_template_function] fptr = fooFunc; // <--


No. A pointer must be a pointer to a specific type. A function
pointer must be a pointer to a specific function signature. Template
instantiations have basically no relationship to each other. Related
types are created by inheritance. What are you trying to accomplish?

Jul 22 '05 #3
Suzanne Vogel wrote:
Is it possible to store a pointer to a template function?

eg,
template<class T> fooFunc() {...}

[ptr_to_template_function] fptr = fooFunc; // <--
...


Strictly speaking, 'fooFunc' is not a template function. It is a
function template. It is not possible to have a pointer to function
template. However, once the template is specialized it becomes ordinary
function and you can create a pointer to it.

There are different ways to specialize a function template. It can be
done by explicit specification of template arguments. Or it can be done
implicitly by compiler (template argument deduction). In fact, when you
do something like this

SomePtrToFunctionType fptr = fooFunc;

(where 'fooFunc' is a function template) the compiler will attempt to
perform template argument deduction based on the target type
('SomePtrToFunctionType') in order to obtain compatible specialization
of 'fooFunc' template.

But again, even if it works, if creates a pointer to a concrete
specialization of the template, not to the function template itself.
There is no such thing in C++ as 'pointer to template'.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #4
Thanks to all those who responded...

David Harmon wrote:
On Fri, 16 Jan 2004 18:46:37 -0500 in comp.lang.c++, Suzanne Vogel
<su*************@hotmail.com> was alleged to have written:
Is it possible to store a pointer to a template function?

eg,
template<class T> fooFunc() {...}

[ptr_to_template_function] fptr = fooFunc; // <--

No. A pointer must be a pointer to a specific type. A function
pointer must be a pointer to a specific function signature. Template
instantiations have basically no relationship to each other. Related
types are created by inheritance. What are you trying to accomplish?


Okay, thanks.

Here's what I was trying to accomplish: I wanted to store a hashmap of
pointers to template functions, indexed by classname:

std::hash_map< std::string, [ptr_to_template_func] > mFuncPtrs;

Each of these pointers to a template function came from here:

template<class T>
class Wrapper
{
public:
template<class S>
void f() {.....} //acts on types S and T together
}

To store a pointer to a template function for class Foo, I'd do this:

class Foo {...}

mFuncPtrs["Foo"] = [ptr_to_Wrapper<Foo>::f()];

But, apparently this is impossible.

Suzanne

Jul 22 '05 #5
On Sat, 17 Jan 2004 01:23:37 -0500 in comp.lang.c++, Suzanne Vogel
<su*************@hotmail.com> was alleged to have written:
Here's what I was trying to accomplish: I wanted to store a hashmap of
pointers to template functions, indexed by classname:

std::hash_map< std::string, [ptr_to_template_func] > mFuncPtrs;


OK, I don't know if this idea gets you closer or not:
Create a base class. The function you are going to call is a virtual
function in the base class. The template classes are derived from the
base, and override the virtual function. The map contains (base*)
pointers.

I don't see how you intend to get from the function pointer to a
type-safe invocation of it. If you wish to throw away type safety, then
there are plenty of ways.

Jul 22 '05 #6

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

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
4
by: Vijai Kalyan | last post by:
I was decomposing a task into different policies. Essentially, there is a general option obtained from a server and user options obtained from configuration variables. The two options are...
15
by: ajj | last post by:
Hello All, Yes this is homework, but I have spent a lot of time on it and I am close. I want to be able to count the number of nodes in a tree that have only one child. I can identify the...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
3
by: .rhavin grobert | last post by:
guess you have the following: _________________________________________________ template <class T> class CQVector { public: // find an element, returns index or -1 if none is found int...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.