473,587 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template functions vs. function objects

Hi
Can someone please write some "compare and contrast" notes for
"Template functions vs. function objects"? When to use one and not the
other? For example, the TF square_f does the same thing as the FO
square_o in the following. When MUST you use one over the other, and
when MIGHT you use one over the other?
Thanks a bunch.....

#include <iostream.h>

template<class T> class Square {
public:
T operator()(cons t T& t) {
return t*t;
}
};

template<class T> T square_f(const T& t) {
return t*t;
}

int main(int argc, char **argv) {

Square<int> square_o;
cout << square_o(5);
cout << square_f(5);

return 0;
}

Jul 23 '05 #1
4 1419
> Can someone please write some "compare and contrast" notes for
"Template functions vs. function objects"? When to use one and not the
other? For example, the TF square_f does the same thing as the FO
square_o in the following. When MUST you use one over the other, and
when MIGHT you use one over the other?
Thanks a bunch.....


Function objects can be created, passed as parameters, or have their state
modified

Since a function object is an object, it can have unique state. Function
objects
can maintain that state between calls. This is nice in multi-thread
environments.

Function object are used in the standard library, where the member function,
say, operator ()(T&), can be inlined, avoiding the overhead of a function
call.
Jul 23 '05 #2
ri***********@y ahoo.co.uk wrote:
Can someone please write some "compare and contrast" notes for
"Template functions vs. function objects"? When to use one and not the
other? For example, the TF square_f does the same thing as the FO
square_o in the following. When MUST you use one over the other, and
when MIGHT you use one over the other?
Is this homework? Smells like homework.

The most noticeable difference is that FO can have state and keep it
between uses. TFs essentially cannot. That makes it possible to provide
additional "arguments" to the operation which they are to perform without
changing the interface of the actual caller.
[...]

Jul 23 '05 #3
Thanks - I am definitely too old for it to be homework though. What I
was trying to understand was this piece of text from
http://pages.cpsc.ucalgary.ca/~kremer/. Please see the question I have
marked ==>> below:

BEGIN QUOTE:

Function Objects work by overloading operator().

Example:
#include < iostream.h >

template < class T >
struct square {
T operator()(T n) {return n*n;}
};

template < class T, class funcObj >
void printEval(T val,funcObj f) {
cout << f(val) << endl;
};

main() {
printEval(1.4, square < float > ());
printEval(1.4, square < int > ());
return 0;
}

output:

1.96
1

Very useful for generating needed functions on-the-fly. (Can't use
template functions for this since we can't specify the template
arguments.)
END QUOTE
==>> I must be in thick mode tonight, I can't get my head round the bit
in brackets ("Can't use.......argum ents"). Too much red wine....

Jul 23 '05 #4
ri***********@y ahoo.co.uk wrote:
Thanks - I am definitely too old for it to be homework though. What I
was trying to understand was this piece of text from
http://pages.cpsc.ucalgary.ca/~kremer/. Please see the question I have
marked ==>> below:

BEGIN QUOTE:

Function Objects work by overloading operator().

Example:
#include < iostream.h >
#include <iostream>
using namespace std;

template < class T >
struct square {
T operator()(T n) {return n*n;}
};

template < class T, class funcObj >
void printEval(T val,funcObj f) {
cout << f(val) << endl;
};

main() {
int main() {
printEval(1.4, square < float > ());
printEval(1.4, square < int > ());
return 0;
}

output:

1.96
1

Very useful for generating needed functions on-the-fly. (Can't use
template functions for this since we can't specify the template
arguments.)
Huh?
END QUOTE
==>> I must be in thick mode tonight, I can't get my head round the bit
in brackets ("Can't use.......argum ents"). Too much red wine....


I think the author you're quoting is simply wrong here. It is possible
he/she is using some hopelessly outdated compiler. Here is the same code
with the function 'square' instead of a type:
-----------------------------
#include <iostream>

template < class T > T square(T n) { return n*n; }

template < class T, class funcObj >
void printEval(T val,funcObj f) {
std::cout << f(val) << std::endl;
};

int main() {
printEval(1.4, square < float >);
printEval(1.4, square < int >);
return 0;
}
-----------------------------

Victor
Jul 23 '05 #5

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

Similar topics

2
5768
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I...
0
1628
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an internal interactive debugger that knows the classes within our system and allow us to walk around the objects that exist at the time of the...
9
2301
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the same type for a given instance of the class, but different types for different instances.) #include<set> using namespace std; template<class T>
6
9252
by: Brian Ross | last post by:
Hi, I am trying to do something similar to the following: int Func1(int x, int y); double Func2(double x, double y); template <typename FuncT> // <- What would go here class CObjectT {
12
2608
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with...
5
2259
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...
2
1808
by: Nike | last post by:
I have a small question w.r.t usage of default arguements in template.. I shall try to elaborate this with an example.. let's say I have some template function , where EntryType is the input for the template fn 1.. and another type where EntryType and lass P1 are both inputs.. case1 (1)template<class EntryType_> i.e void ...
8
1966
by: Belebele | last post by:
Suppose that I want to write a (concrete) interface class (without virtual functions) to classes that contain a method template // -------------------------------------- class Interface { public: template <typename Wrapped> Interface(Wrapped& ); // wraps a reference to another object // that implements the method template
5
1765
by: Szabolcs | last post by:
I am looking for a way to generalize the function template below, so that it will work with any type, not just double. Is this at all possible in C++? I'd like to replace double (*fun)(double) with a generalized A (*fun)(B). template<double (*fun)(double)> array<doubleapply(const array<double&source) {...
0
7918
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...
0
8206
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. ...
1
7967
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...
1
5713
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
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...
0
3840
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...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
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
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.