Connecting Tech Pros Worldwide Forums | Help | Site Map

Template specialization of pointers with function pointers

Phil
Guest
 
Posts: n/a
#1: Jul 19 '05
Ok, I have a template function for any pointer to type T:

template <typename T>
void func(T* p)
{
DoSomethingGeneric(p);
}

Can I specialize this template for pointers to functions (or pointers
to member functions, or pointers to anything)? I would like to do
something like this:

typedef void (*FUNCPTR)();

template <>
void func(FUNCPTR p)
{
DoSomethingSpecialWithFuncPtr(p);
}

but my compiler (Visual C++ 6) won't let me. I know VC6 is lacking in
support for templates, but I'd like to know if this is even legal
anyway. Thanks.

llewelly
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Template specialization of pointers with function pointers


cognito79@hotmail.com (Phil) writes:
[color=blue]
> Ok, I have a template function for any pointer to type T:
>
> template <typename T>
> void func(T* p)
> {
> DoSomethingGeneric(p);
> }
>
> Can I specialize this template for pointers to functions (or pointers
> to member functions, or pointers to anything)? I would like to do
> something like this:
>
> typedef void (*FUNCPTR)();
>
> template <>
> void func(FUNCPTR p)
> {
> DoSomethingSpecialWithFuncPtr(p);
> }[/color]

Yes. This compiles as-is with most modern compilers,
[color=blue]
>
> but my compiler (Visual C++ 6) won't let me.[/color]

It's time you got out of the pleistocene and got into the
holocene. You can get a better compiler from www.mingw.org,
www.bloodshed.net, and many other places. (You can even get one
from M$, provided you pay enough.)
[color=blue]
> I know VC6 is lacking in
> support for templates, but I'd like to know if this is even legal
> anyway.[/color]

It's well-formed.
Closed Thread