pointer to template function

S

Suzanne Vogel

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
 
G

Gianni Mariani

Suzanne said:
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)();

}
 
D

David Harmon

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?
 
A

Andrey Tarasevich

Suzanne said:
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'.
 
S

Suzanne Vogel

Thanks to all those who responded...

David said:
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
 
D

David Harmon

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top