473,500 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer to function template parameter

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.
Sep 3 '05 #1
4 1547

"Amadeus W. M." <am*******@cablespeed.com> wrote in message
news:pa****************************@cablespeed.com ...
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
Sep 3 '05 #2
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
}
Sep 3 '05 #3
Amadeus W. M. wrote:
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
Sep 3 '05 #4
On Sat, 03 Sep 2005 05:48:08 +0000, John Harrison wrote:
Amadeus W. M. wrote:
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


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

Sep 7 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1837
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in...
4
2228
by: firegun9 | last post by:
Hello everyone, here is my program: /////////////////////////////// #include <iostream> using namespace std; void multi(double* arrayPtr, int len){ for(int i=0; i<len; i++)...
110
9809
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
15
2203
by: ajj | last post by:
Hello All, Yes this is homework, but I have spent a lot of time on it and I am close. I want to be able to count the number of nodes in a tree that have only one child. I can identify the...
11
3404
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
5
2247
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
2
2911
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
7
2740
by: agitlin | last post by:
Hello All, I'm working with a SDK for a piece of hardware attached to a Windows CE device. The sample code provided is all written in eVC++ and I'm trying to port it over to a VB.NET...
5
3673
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
50
4424
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.