Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pointer to function template parameter

Reply
Thread Tools

pointer to function template parameter

 
 
Amadeus W. M.
Guest
Posts: n/a
 
      09-03-2005
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.


 
Reply With Quote
 
 
 
 
Thierry Miceli
Guest
Posts: n/a
 
      09-03-2005
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



 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      09-03-2005

"Amadeus W. M." <(E-Mail Removed)> wrote in message
news(E-Mail Removed) ...
> 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


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      09-03-2005
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
 
Reply With Quote
 
Amadeus W. M.
Guest
Posts: n/a
 
      09-07-2005
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!



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function pointer as template argument - does not work for function template avasilev C++ 4 12-22-2011 02:11 PM
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
pointer to function as template parameter MaxMax C++ 3 05-28-2006 03:36 PM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments