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. 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
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
}
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
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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
|
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;
|
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
|
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
|
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.
...
| |
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...
|
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
{
|
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...
|
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...
|
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*);
|
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...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
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...
| |