473,625 Members | 3,254 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 1554

"Amadeus W. M." <am*******@cabl espeed.com> wrote in message
news:pa******** *************** *****@cablespee d.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(doub le 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(doub le 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
1851
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 include files: typedef Context *Context_ptr; typedef ObjOut<Context> Context_out;
4
2248
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++) *(arrayPtr+i)*=2;
110
9893
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 must be an object instead of
15
2224
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 nodes with the following code, but I don't know how to sum them up and return that value to the calling function in main(). I know the algorithm is recursive in nature, but I get gibberish for
11
3423
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 exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
5
2262
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 to functions are hard to inline for the compiler, and so you won't be able to avoid function call overhead. This is an important aspect when you are calling a function very frequently for evaluation reason: think of the problem of performing...
2
2923
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
2757
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 application we're writing. Up to this point I've been able to translate the code into VB.NET, but one function (BK_EXTRACT) from the SDK has me perplexed. In the interest of disclosure, I've barely got my head wrapped around pointers. I'm sure what I'm...
5
3689
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 explicitly takes a pointer-to-member with no parameters, why is the lookup for the overloaded function not able to use the number of arguments to determine the appropriate function? Is there a relevant portion of the standard that governs the...
50
4468
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): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
8256
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8694
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4193
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1500
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.