By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,315 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,315 IT Pros & Developers. It's quick & easy.

pointer to template function

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.