store pointer to template function?

S

Suzanne Vogel

Is it possible to store a pointer to a template function whose template
type is not known until runtime?

eg,
Can I store a pointer to this?

template<class T> f() {...}

I know how to store pointers to ordinary functions, but not template
functions.

Thanks,
Suzanne
 
J

Jonathan Turkanis

Suzanne Vogel said:
Is it possible to store a pointer to a template function whose template
type is not known until runtime?

eg,
Can I store a pointer to this?

template<class T> f() {...}

Unfortunately, no. You can store pointers to specializations of
template functions, but not the actual template. If you give an
example of your intended use, perhaps we can suggest another way to
accomplish the same thing.
I know how to store pointers to ordinary functions, but not template
functions.

They're stored just like other function pointer. You have to be
careful about getting the address of the correct overload.

Jonathan
 
R

Ron Natalie

Suzanne Vogel said:
Is it possible to store a pointer to a template function whose template
type is not known until runtime?

eg,
Can I store a pointer to this?

template<class T> f() {...}
No. Templates are instantiated at build time.
You can only get pointers to specific specializations.
 
G

Gianni Mariani

Ron said:
No. Templates are instantiated at build time.
You can only get pointers to specific specializations.

Except in this case, the function signature is independant of the
template argument. (and the syntax is incorrect too - but that's a
different issue)

This works:
(assuming the template function returns an int)

int (* ptr_fooFunc)() = &f<x>;
 
R

Ron Natalie

Gianni Mariani said:
Except in this case, the function signature is independant of the
template argument. (and the syntax is incorrect too - but that's a
different issue)

It's still the case. There's no requirement that the function signature reflect the template
args. (Except for bugs in certain compilers like VC++ 6). It just means you have to
provide the explicit template argument list.
This works:
(assuming the template function returns an int)

int (* ptr_fooFunc)() = &f<x>;
Doesn't solve the problem the original poster asked for. They wanted to bind the function
by pointer later to a specific specialization. This binds it to the f<x> specialization at initialization
time.
 
S

Suzanne Vogel

Ron said:
Doesn't solve the problem the original poster asked for. They wanted to bind the function
by pointer later to a specific specialization. This binds it to the f<x> specialization at initialization
time.

Correct: doesn't solve the problem. Thanks, Ron. And thanks Gianni, for
the suggestion.

Suzanne
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top