pointer to function template parameter

A

Amadeus W. M.

Don't laugh/get pissed off if I'm way off here, but is it possible to have
a template parameter a pointer to a function? If it's possible, an example
would be good.

I'm getting tired of implementing functors as wrappers for what should be
simple C functions.
 
T

Thierry Miceli

Hello Amadeus W. M.,
Don't laugh/get pissed off if I'm way off here, but is it possible to
have a template parameter a pointer to a function? If it's possible,
an example would be good.

I'm getting tired of implementing functors as wrappers for what should
be simple C functions.

You should be able to pass a pointer to a function where you pass a functor.

example:

template< typename T > void foo( T fun )
{
fun();
}

void bar()
{
}

struct functor
{
void operator()();
};

main()
{
foo(bar); // pass pointer to function
foo(functor()); // pass functor instance
 
B

benben

Amadeus W. M. said:
Don't laugh/get pissed off if I'm way off here, but is it possible to have
a template parameter a pointer to a function? If it's possible, an example
would be good.

I'm getting tired of implementing functors as wrappers for what should be
simple C functions.


AFAIK a simple C function is a valid functor.

Ben
 
J

John Harrison

Amadeus said:
Don't laugh/get pissed off if I'm way off here, but is it possible to have
a template parameter a pointer to a function? If it's possible, an example
would be good.

I'm getting tired of implementing functors as wrappers for what should be
simple C functions.

Is this what you mean?

#include <amth.h>

template <double (*func)(double)>
double wierd_trig(double x)
{
return func(x);
}

int main()
{
wierd_trig<sin>(1.0);
wierd_trig<cos>(1.0);
}

Template terminology is confusing, but I believe that is what you asked.

john
 
A

Amadeus W. M.

Is this what you mean?

#include <amth.h>

template <double (*func)(double)>
double wierd_trig(double x)
{
return func(x);
}

int main()
{
wierd_trig<sin>(1.0);
wierd_trig<cos>(1.0);
}

Template terminology is confusing, but I believe that is what you asked.

john

That's precisely what I mean, thanks a lot!
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top