Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   pointer to function template parameter (http://www.velocityreviews.com/forums/t448120-pointer-to-function-template-parameter.html)

Amadeus W. M. 09-03-2005 02:34 AM

pointer to function template parameter
 
Don't laugh/get ****ed 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.



Thierry Miceli 09-03-2005 02:53 AM

Re: pointer to function template parameter
 
Hello Amadeus W. M.,

> Don't laugh/get ****ed 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




benben 09-03-2005 03:03 AM

Re: pointer to function template parameter
 

"Amadeus W. M." <amadeus84@cablespeed.com> wrote in message
news:pan.2005.09.03.02.34.49.506935@cablespeed.com ...
> Don't laugh/get ****ed 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



John Harrison 09-03-2005 05:48 AM

Re: pointer to function template parameter
 
Amadeus W. M. wrote:
> Don't laugh/get ****ed 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

Amadeus W. M. 09-07-2005 09:18 PM

Re: pointer to function template parameter
 
On Sat, 03 Sep 2005 05:48:08 +0000, John Harrison wrote:

> Amadeus W. M. wrote:
>> Don't laugh/get ****ed 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


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





All times are GMT. The time now is 09:49 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.